2
0

consoleLogger.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleLogger.h"
  23. #include "console/consoleTypes.h"
  24. Vector<ConsoleLogger *> ConsoleLogger::mActiveLoggers;
  25. bool ConsoleLogger::smInitialized = false;
  26. IMPLEMENT_CONOBJECT( ConsoleLogger );
  27. //-----------------------------------------------------------------------------
  28. ConsoleLogger::ConsoleLogger()
  29. {
  30. mFilename = NULL;
  31. mLogging = false;
  32. mAppend = false;
  33. mLevel = ConsoleLogEntry::Normal;
  34. }
  35. //-----------------------------------------------------------------------------
  36. ConsoleLogger::ConsoleLogger( const char *fileName, bool append )
  37. {
  38. mLogging = false;
  39. mLevel = ConsoleLogEntry::Normal;
  40. mFilename = StringTable->insert( fileName );
  41. mAppend = append;
  42. init();
  43. }
  44. //-----------------------------------------------------------------------------
  45. static EnumTable::Enums logLevelEnums[] =
  46. {
  47. { ConsoleLogEntry::Normal, "normal" },
  48. { ConsoleLogEntry::Warning, "warning" },
  49. { ConsoleLogEntry::Error, "error" },
  50. };
  51. static EnumTable gLogLevelTable( 3, &logLevelEnums[0] );
  52. void ConsoleLogger::initPersistFields()
  53. {
  54. Parent::initPersistFields();
  55. addGroup( "Logging" );
  56. addField( "level", TypeEnum, Offset( mLevel, ConsoleLogger ), 1, &gLogLevelTable );
  57. endGroup( "Logging" );
  58. }
  59. //-----------------------------------------------------------------------------
  60. bool ConsoleLogger::processArguments( S32 argc, const char **argv )
  61. {
  62. if( argc == 0 )
  63. return false;
  64. bool append = false;
  65. if( argc == 2 )
  66. append = dAtob( argv[1] );
  67. mAppend = append;
  68. mFilename = StringTable->insert( argv[0] );
  69. if( init() )
  70. {
  71. attach();
  72. return true;
  73. }
  74. return false;
  75. }
  76. //-----------------------------------------------------------------------------
  77. ConsoleLogger::~ConsoleLogger()
  78. {
  79. detach();
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool ConsoleLogger::init()
  83. {
  84. if( smInitialized )
  85. return true;
  86. Con::addConsumer( ConsoleLogger::logCallback );
  87. smInitialized = true;
  88. return true;
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool ConsoleLogger::attach()
  92. {
  93. if( mFilename == NULL )
  94. {
  95. Con::errorf( "ConsoleLogger failed to attach: no filename supplied." );
  96. return false;
  97. }
  98. // Check to see if this is initialized before using it
  99. if( !smInitialized )
  100. {
  101. if( !init() )
  102. {
  103. Con::errorf( "ConsoleLogger failed to initalize." );
  104. return false;
  105. }
  106. }
  107. if( mLogging )
  108. return false;
  109. // Open the filestream
  110. mStream.open( mFilename, ( mAppend ? FileStream::WriteAppend : FileStream::Write ) );
  111. // Add this to list of active loggers
  112. mActiveLoggers.push_back( this );
  113. mLogging = true;
  114. return true;
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool ConsoleLogger::detach()
  118. {
  119. // Make sure this is valid before messing with it
  120. if( !smInitialized )
  121. {
  122. if( !init() )
  123. {
  124. return false;
  125. }
  126. }
  127. if( !mLogging )
  128. return false;
  129. // Close filestream
  130. mStream.close();
  131. // Remove this object from the list of active loggers
  132. for( int i = 0; i < mActiveLoggers.size(); i++ )
  133. {
  134. if( mActiveLoggers[i] == this )
  135. {
  136. mActiveLoggers.erase( i );
  137. mLogging = false;
  138. return true;
  139. }
  140. }
  141. return false; // If this happens, it's bad...
  142. }
  143. //-----------------------------------------------------------------------------
  144. void ConsoleLogger::logCallback( ConsoleLogEntry::Level level, const char *consoleLine )
  145. {
  146. ConsoleLogger *curr;
  147. // Loop through active consumers and send them the message
  148. for( int i = 0; i < mActiveLoggers.size(); i++ )
  149. {
  150. curr = mActiveLoggers[i];
  151. // If the log level is within the log threshhold, log it
  152. if( curr->mLevel <= level )
  153. curr->log( consoleLine );
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. void ConsoleLogger::log( const char *consoleLine )
  158. {
  159. // Check to see if this is intalized before using it
  160. if( !smInitialized )
  161. {
  162. if( !init() )
  163. {
  164. Con::errorf( "I don't know how this happened, but log called on this without it being initialized" );
  165. return;
  166. }
  167. }
  168. mStream.writeLine( (U8 *)consoleLine );
  169. }
  170. //-----------------------------------------------------------------------------
  171. ConsoleMethod( ConsoleLogger, attach, bool, 2, 2, "() Attaches this object to the console and begins logging\n"
  172. "@return Returns true on success, and false on failure.")
  173. {
  174. ConsoleLogger *logger = static_cast<ConsoleLogger *>( object );
  175. return logger->attach();
  176. }
  177. //-----------------------------------------------------------------------------
  178. ConsoleMethod( ConsoleLogger, detach, bool, 2, 2, "() Detaches this object from the console and stops logging\n"
  179. "@return Returns true on success, and false on failure.")
  180. {
  181. ConsoleLogger *logger = static_cast<ConsoleLogger *>( object );
  182. return logger->detach();
  183. }