home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / log.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-18  |  6.2 KB  |  226 lines

  1. /*!
  2. \file log.cpp
  3. \author Karsten Schwenk
  4.  
  5.  
  6. See header for description (log.h).
  7. */
  8.  
  9.  
  10. #include "log.h"
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include "ConsoleFrontEnd.h"
  16. #include "System.h"
  17.  
  18. #include "Gui.h"
  19.  
  20. #ifndef WIN32
  21. #include <stdarg.h>
  22. #endif
  23.  
  24. #define LOG_STRING_BUFFER_SIZE    1024
  25.  
  26. static FILE *logFile=NULL;    //!< our logfile, where all log-, error- and warning messages are logged
  27.  
  28. /*!
  29. This functions opens a new logfile named 'lastrun.log' and writes a short header with the date into it.
  30. This should be called as one of the first things. Older files are overwritten.
  31. If 'lastrun.log' couldn't be opened, #logFile is set to \c NULL and calls to #log(), #error() and #warn() will only write on the screen, but not to a file.
  32. */
  33. void beginLog(){
  34.     time_t ltime;
  35.  
  36.     if(logFile!=NULL){
  37.         error("(in log::beginLog()): There seems to be another logging session in progress.\n\n");
  38.         //endLog();
  39.         return;
  40.     }
  41.  
  42.     logFile=fopen("lastrun.log", "w+");
  43.     if(logFile==NULL){
  44.         printf("Couldn't open logFile.\n");
  45.         return;
  46.     }
  47.     log("*************************\n");
  48.     log("***** BEGINNING LOG *****\n");
  49.     log("*************************\n");
  50.  
  51.     time( <ime );
  52.     log("\nTime: %s\n", ctime( <ime ) );
  53. }
  54.  
  55. /*!
  56. This functions closes #logFile (if it was opened).
  57. Note that calls to #log(), #error() and #warn() are still allowed, but they will only write on the screen.
  58. This function should be called whenever you leave the game and have called #beginLog() before (and then as one of the last things...).
  59. */
  60. void endLog(){
  61.     time_t ltime;
  62.  
  63.     if(logFile==NULL){
  64.         error("(in log::endLog()): No logging session in progress.\n\n");
  65.         return;
  66.     }
  67.  
  68.     log("\n");
  69.     log("===================\n");
  70.     log("=== CLOSING LOG ===\n");
  71.     log("===================\n");
  72.  
  73.     time( <ime );
  74.     log("\nTime: %s\n", ctime( <ime ) );
  75.     fclose(logFile);
  76.     logFile=NULL;
  77. }
  78.  
  79. /*!
  80. This function prints a message to stdout, #logFile (if opened), the #console (if initialized), #hud::messageBox (if initialized) and updates the #loadingMenu (if initialized and opened). This should be used to indicate some forthcoming to the user, e.g. after something has been loaded etc.
  81. \param formatString a standard \c printf format string
  82. */
  83. void log(char *formatString, ...){
  84.     char toStringBuffer[LOG_STRING_BUFFER_SIZE];
  85.     char str[LOG_STRING_BUFFER_SIZE];
  86.     str[0]='\0';
  87.  
  88.     va_list ap;
  89.  
  90.     va_start (ap, formatString);
  91.     vsprintf(toStringBuffer, formatString, ap);
  92.     va_end (ap);
  93.  
  94.     strcat(str, toStringBuffer);
  95.  
  96.     printf(str);
  97.  
  98.     if(logFile!=NULL)
  99.         fprintf(logFile, str);
  100.  
  101.     if(console!=NULL)
  102.         console->print(str);
  103.  
  104.     if(Gui::loadingMenu!=NULL && Gui::loadingMenu->isOpened())
  105.         Gui::loadingMenu->update();
  106.  
  107.     if(Gui::hud!=NULL && Gui::hud->messagebox!=NULL)
  108.         Gui::hud->messagebox->addMessage(new Message(str));
  109. }
  110.  
  111. /*!
  112. This function prints a warning message to stdout, #logFile (if opened), the #console (if initialized), #hud::messageBox (if initialized) and updates the #loadingMenu (if initialized and opened). This should be used to inform the user of minor errors, that do not have any impact on gameplay but nevertheless are worth noticing (e.g. if an unrecognized token is found in a file).
  113.  
  114. This is used mainly during debugging.
  115. \param formatString a standard \c printf format string
  116. */
  117. void warn(char* formatString, ...){
  118.     char toStringBuffer[LOG_STRING_BUFFER_SIZE];
  119.     char str[LOG_STRING_BUFFER_SIZE];
  120.     strcpy(str, "\n^5## WARNING: ");
  121.  
  122.     va_list ap;
  123.  
  124.     va_start (ap, formatString);
  125.     vsprintf(toStringBuffer, formatString, ap);
  126.     va_end (ap);
  127.  
  128.     strcat(str, toStringBuffer);
  129.  
  130.     printf(str);
  131.  
  132.     if(logFile!=NULL)
  133.         fprintf(logFile, str);
  134.  
  135.     if(console!=NULL)
  136.         console->print(str);
  137.  
  138.     if(Gui::loadingMenu!=NULL && Gui::loadingMenu->isOpened())
  139.         Gui::loadingMenu->update();
  140.  
  141.     if(Gui::hud!=NULL && Gui::hud->messagebox!=NULL)
  142.         Gui::hud->messagebox->addMessage(new Message(str));
  143. }
  144.  
  145. /*!
  146. This function prints an error message to stdout, #logFile (if opened), the #console (if initialized), #hud::messageBox (if initialized) and updates the #loadingMenu (if initialized and opened). This should be used to inform the user of serious errors, that may make the game unplayable, although they aren't a real reason to quit (e.g. if a texture or a model couldn't be loaded).
  147.  
  148. For really fatal errors, that to not allow the game to proceed, use #fatal().
  149. \param formatString a standard \c printf format string
  150. */
  151. void error(char* formatString, ...){
  152.     char toStringBuffer[LOG_STRING_BUFFER_SIZE];
  153.     char str[LOG_STRING_BUFFER_SIZE];
  154.     strcpy(str, "\n^2## ERROR: ");
  155.   
  156.     va_list ap;
  157.  
  158.     va_start (ap, formatString);
  159.     vsprintf(toStringBuffer, formatString, ap);
  160.     va_end (ap);
  161.  
  162.     strcat(str, toStringBuffer);
  163.  
  164.     printf(str);
  165.  
  166.     if(logFile!=NULL)
  167.         fprintf(logFile, str);
  168.  
  169.     if(console!=NULL)
  170.         console->print(str);
  171.  
  172.     if(Gui::loadingMenu!=NULL && Gui::loadingMenu->isOpened())
  173.         Gui::loadingMenu->update();
  174.  
  175.     if(Gui::hud!=NULL && Gui::hud->messagebox!=NULL)
  176.         Gui::hud->messagebox->addMessage(new Message(str));
  177.  
  178.     if(Gui::errorMenu!=NULL){
  179.         Gui::errorMenu->setErrorString(str);
  180.         Gui::errorMenu->open();    // ...and wait until it closes again...
  181.     }
  182. }
  183.  
  184. /*!
  185. This function should be used to when a really bad error has occured and the game MUST quit.
  186. It prints an errormessage to stdout and the #logFile and then quits the game immediately by calling the #errorQuit() function.
  187. Under Windows a MessageBox is opened, too.
  188.  
  189. \param formatString a standard \c printf format string
  190. */
  191. void fatal(char* formatString, ...){
  192.     char toStringBuffer[LOG_STRING_BUFFER_SIZE];
  193.     char str[LOG_STRING_BUFFER_SIZE];
  194.     strcpy(str, "\n^2## FATAL ERROR: ");
  195.   
  196.     va_list ap;
  197.  
  198.     va_start (ap, formatString);
  199.     vsprintf(toStringBuffer, formatString, ap);
  200.     va_end (ap);
  201.  
  202.     strcat(str, toStringBuffer);
  203.  
  204.     printf(str);
  205.  
  206.     if(logFile!=NULL)
  207.         fprintf(logFile, str);
  208.  
  209. #ifdef WIN32
  210.     MessageBox(NULL, str, "FWP: Fatal Error", MB_OK | MB_ICONERROR);
  211. #endif
  212.  
  213. /*
  214.     if(console!=NULL)
  215.         console->print(str);
  216.  
  217.     if(loadingMenu!=NULL && loadingMenu->isOpened())
  218.         loadingMenu->update();
  219.  
  220.     if(hud!=NULL && hud->messageBox!=NULL)
  221.         hud->messageBox->addMessage(new Message(str));
  222. */
  223.     System::errorQuit();
  224. }
  225.  
  226.