consoleLogger.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "console/engineAPI.h"
  25. Vector<ConsoleLogger *> ConsoleLogger::mActiveLoggers;
  26. bool ConsoleLogger::smInitialized = false;
  27. IMPLEMENT_CONOBJECT( ConsoleLogger );
  28. ConsoleDocClass( ConsoleLogger,
  29. "A class designed to be used as a console consumer and log the data it receives to a file.\n\n"
  30. "@see dumpConsoleFunctions\n"
  31. "@see dumpConsoleClasses\n"
  32. "@ingroup Logging\n"
  33. );
  34. //-----------------------------------------------------------------------------
  35. ConsoleLogger::ConsoleLogger()
  36. {
  37. mFilename = NULL;
  38. mLogging = false;
  39. mAppend = false;
  40. mLevel = ConsoleLogEntry::Normal;
  41. }
  42. //-----------------------------------------------------------------------------
  43. ConsoleLogger::ConsoleLogger( const char *fileName, bool append )
  44. {
  45. mLogging = false;
  46. mLevel = ConsoleLogEntry::Normal;
  47. mFilename = StringTable->insert( fileName );
  48. mAppend = append;
  49. init();
  50. }
  51. //-----------------------------------------------------------------------------
  52. ImplementEnumType( LogLevel,
  53. "@brief Priority levels for logging entries\n\n"
  54. "@ingroup Logging")
  55. { ConsoleLogEntry::Normal, "normal", "Lowest priority level, no highlighting." },
  56. { ConsoleLogEntry::Warning, "warning", "Mid level priority, tags and highlights possible issues in blue." },
  57. { ConsoleLogEntry::Error, "error", "Highest priority level, extreme emphasis on this entry. Highlighted in red." },
  58. EndImplementEnumType;
  59. void ConsoleLogger::initPersistFields()
  60. {
  61. addGroup( "Logging" );
  62. addField( "level", TYPEID< ConsoleLogEntry::Level >(), Offset( mLevel, ConsoleLogger ), "Determines the priority level and attention the logged entry gets when recorded\n\n" );
  63. endGroup( "Logging" );
  64. Parent::initPersistFields();
  65. }
  66. //-----------------------------------------------------------------------------
  67. bool ConsoleLogger::processArguments( S32 argc, ConsoleValue *argv )
  68. {
  69. if( argc == 0 )
  70. return false;
  71. bool append = false;
  72. if (argc == 2)
  73. append = argv[1].getBool();
  74. mAppend = append;
  75. mFilename = StringTable->insert( argv[0].getString() );
  76. if( init() )
  77. {
  78. attach();
  79. return true;
  80. }
  81. return false;
  82. }
  83. //-----------------------------------------------------------------------------
  84. ConsoleLogger::~ConsoleLogger()
  85. {
  86. detach();
  87. }
  88. //-----------------------------------------------------------------------------
  89. bool ConsoleLogger::init()
  90. {
  91. if( smInitialized )
  92. return true;
  93. Con::addConsumer( ConsoleLogger::logCallback );
  94. smInitialized = true;
  95. return true;
  96. }
  97. //-----------------------------------------------------------------------------
  98. bool ConsoleLogger::attach()
  99. {
  100. if( mFilename == NULL )
  101. {
  102. Con::errorf( "ConsoleLogger failed to attach: no filename supplied." );
  103. return false;
  104. }
  105. // Check to see if this is initialized before using it
  106. if( !smInitialized )
  107. {
  108. if( !init() )
  109. {
  110. Con::errorf( "ConsoleLogger failed to initalize." );
  111. return false;
  112. }
  113. }
  114. if( mLogging )
  115. return false;
  116. // Open the filestream
  117. mStream.open( mFilename, ( mAppend ? Torque::FS::File::WriteAppend : Torque::FS::File::Write ) );
  118. // Add this to list of active loggers
  119. mActiveLoggers.push_back( this );
  120. mLogging = true;
  121. return true;
  122. }
  123. //-----------------------------------------------------------------------------
  124. bool ConsoleLogger::detach()
  125. {
  126. // Make sure this is valid before messing with it
  127. if( !smInitialized )
  128. {
  129. if( !init() )
  130. {
  131. return false;
  132. }
  133. }
  134. if( !mLogging )
  135. return false;
  136. // Close filestream
  137. mStream.close();
  138. // Remove this object from the list of active loggers
  139. for( S32 i = 0; i < mActiveLoggers.size(); i++ )
  140. {
  141. if( mActiveLoggers[i] == this )
  142. {
  143. mActiveLoggers.erase( i );
  144. mLogging = false;
  145. return true;
  146. }
  147. }
  148. return false; // If this happens, it's bad...
  149. }
  150. //-----------------------------------------------------------------------------
  151. void ConsoleLogger::logCallback( U32 level, const char *consoleLine )
  152. {
  153. ConsoleLogger *curr;
  154. // Loop through active consumers and send them the message
  155. for( int i = 0; i < mActiveLoggers.size(); i++ )
  156. {
  157. curr = mActiveLoggers[i];
  158. // If the log level is within the log threshhold, log it
  159. if( curr->mLevel <= level )
  160. curr->log( consoleLine );
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ConsoleLogger::log( const char *consoleLine )
  165. {
  166. // Check to see if this is intalized before using it
  167. if( !smInitialized )
  168. {
  169. if( !init() )
  170. {
  171. Con::errorf( "I don't know how this happened, but log called on this without it being initialized" );
  172. return;
  173. }
  174. }
  175. mStream.writeLine( (U8 *)consoleLine );
  176. }
  177. //-----------------------------------------------------------------------------
  178. DefineEngineMethod( ConsoleLogger, attach, bool, (), , "() Attaches the logger to the console and begins writing to file"
  179. "@tsexample\n"
  180. "// Create the logger\n"
  181. "// Will automatically start writing to testLogging.txt with normal priority\n"
  182. "new ConsoleLogger(logger, \"testLogging.txt\", false);\n\n"
  183. "// Send something to the console, with the logger consumes and writes to file\n"
  184. "echo(\"This is logged to the file\");\n\n"
  185. "// Stop logging, but do not delete the logger\n"
  186. "logger.detach();\n\n"
  187. "echo(\"This is not logged to the file\");\n\n"
  188. "// Attach the logger to the console again\n"
  189. "logger.attach();\n\n"
  190. "// Logging has resumed\n"
  191. "echo(\"Logging has resumed\");"
  192. "@endtsexample\n\n")
  193. {
  194. ConsoleLogger *logger = static_cast<ConsoleLogger *>( object );
  195. return logger->attach();
  196. }
  197. //-----------------------------------------------------------------------------
  198. DefineEngineMethod( ConsoleLogger, detach, bool, (), , "() Detaches the logger from the console and stops writing to file"
  199. "@tsexample\n"
  200. "// Create the logger\n"
  201. "// Will automatically start writing to testLogging.txt with normal priority\n"
  202. "new ConsoleLogger(logger, \"testLogging.txt\", false);\n\n"
  203. "// Send something to the console, with the logger consumes and writes to file\n"
  204. "echo(\"This is logged to the file\");\n\n"
  205. "// Stop logging, but do not delete the logger\n"
  206. "logger.detach();\n\n"
  207. "echo(\"This is not logged to the file\");\n\n"
  208. "// Attach the logger to the console again\n"
  209. "logger.attach();\n\n"
  210. "// Logging has resumed\n"
  211. "echo(\"Logging has resumed\");"
  212. "@endtsexample\n\n")
  213. {
  214. ConsoleLogger *logger = static_cast<ConsoleLogger *>( object );
  215. return logger->detach();
  216. }