/src/shared/log.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 63 lines · 42 code · 16 blank · 5 comment · 3 complexity · 7b42ac6dd8432f95d915745f01b79cf6 MD5 · raw file

  1. #include "common.h"
  2. #include "log.h"
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <assert.h>
  6. //-----------------------------------------------------------------------------
  7. FILE* log_file = 0;
  8. CRITICAL_SECTION log_mutex;
  9. //-----------------------------------------------------------------------------
  10. int log_open( const char* path )
  11. {
  12. assert(path);
  13. if( log_file )
  14. fclose(log_file);
  15. log_file = fopen(path, "w");
  16. if( !log_file )
  17. return 1;
  18. InitializeCriticalSection( &log_mutex );
  19. return 0;
  20. }
  21. //-----------------------------------------------------------------------------
  22. void log_write( const char* format, ... )
  23. {
  24. assert(format);
  25. assert(log_file);
  26. EnterCriticalSection( &log_mutex );
  27. va_list ap;
  28. va_start(ap, format);
  29. vfprintf(log_file, format, ap);
  30. va_end(ap);
  31. va_start(ap, format);
  32. vfprintf( stderr, format, ap );
  33. va_end(ap);
  34. fprintf(stderr, "\n");
  35. fprintf(log_file, "\n");
  36. fflush(log_file);
  37. LeaveCriticalSection( &log_mutex );
  38. }
  39. //-----------------------------------------------------------------------------
  40. void log_close()
  41. {
  42. DeleteCriticalSection( &log_mutex );
  43. if( log_file )
  44. fclose(log_file);
  45. log_file = 0;
  46. }
  47. //-----------------------------------------------------------------------------