MiniLog.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: MiniLog.cpp ///////////////////////////////////////////////////////////
  24. // Alternative logging
  25. // Author: Matthew D. Campbell, January 2003
  26. ////////////////////////////////////////////////////////////////////////////////
  27. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  28. #include "Common/MiniLog.h"
  29. #ifdef DEBUG_LOGGING
  30. LogClass::LogClass(const char *fname)
  31. {
  32. char buffer[ _MAX_PATH ];
  33. GetModuleFileName( NULL, buffer, sizeof( buffer ) );
  34. char *pEnd = buffer + strlen( buffer );
  35. while( pEnd != buffer )
  36. {
  37. if( *pEnd == '\\' )
  38. {
  39. *pEnd = 0;
  40. break;
  41. }
  42. pEnd--;
  43. }
  44. AsciiString fullPath;
  45. fullPath.format("%s\\%s", buffer, fname);
  46. m_fp = fopen(fullPath.str(), "wt");
  47. }
  48. LogClass::~LogClass()
  49. {
  50. if (m_fp)
  51. {
  52. fclose(m_fp);
  53. }
  54. }
  55. void LogClass::log(const char *fmt, ...)
  56. {
  57. if (!m_fp)
  58. return;
  59. static char buf[1024];
  60. static Int lastFrame = 0;
  61. static Int lastIndex = 0;
  62. if (lastFrame != TheGameLogic->getFrame())
  63. {
  64. lastFrame = TheGameLogic->getFrame();
  65. lastIndex = 0;
  66. }
  67. va_list va;
  68. va_start( va, fmt );
  69. _vsnprintf(buf, 1024, fmt, va );
  70. buf[1023] = 0;
  71. va_end( va );
  72. char *tmp = buf;
  73. while (tmp && *tmp)
  74. {
  75. if (*tmp == '\r' || *tmp == '\n')
  76. {
  77. *tmp = ' ';
  78. }
  79. ++tmp;
  80. }
  81. fprintf(m_fp, "%d:%d %s\n", lastFrame, lastIndex++, buf);
  82. fflush(m_fp);
  83. }
  84. #endif // DEBUG_LOGGING