consoleLogger.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "consoleLogger_ScriptBinding.h"
  25. Vector<ConsoleLogger *> ConsoleLogger::mActiveLoggers;
  26. bool ConsoleLogger::smInitialized = false;
  27. IMPLEMENT_CONOBJECT( ConsoleLogger );
  28. //-----------------------------------------------------------------------------
  29. ConsoleLogger::ConsoleLogger()
  30. {
  31. mFilename = NULL;
  32. mLogging = false;
  33. mAppend = false;
  34. mLevel = ConsoleLogEntry::Normal;
  35. }
  36. //-----------------------------------------------------------------------------
  37. ConsoleLogger::ConsoleLogger( const char *fileName, bool append )
  38. {
  39. mLogging = false;
  40. mLevel = ConsoleLogEntry::Normal;
  41. mFilename = StringTable->insert( fileName );
  42. mAppend = append;
  43. init();
  44. }
  45. //-----------------------------------------------------------------------------
  46. static EnumTable::Enums logLevelEnums[] =
  47. {
  48. { ConsoleLogEntry::Normal, "normal" },
  49. { ConsoleLogEntry::Warning, "warning" },
  50. { ConsoleLogEntry::Error, "error" },
  51. };
  52. static EnumTable gLogLevelTable( 3, &logLevelEnums[0] );
  53. void ConsoleLogger::initPersistFields()
  54. {
  55. Parent::initPersistFields();
  56. addGroup( "Logging" );
  57. addField( "level", TypeEnum, Offset( mLevel, ConsoleLogger ), 1, &gLogLevelTable );
  58. endGroup( "Logging" );
  59. }
  60. //-----------------------------------------------------------------------------
  61. bool ConsoleLogger::processArguments( S32 argc, const char **argv )
  62. {
  63. if( argc == 0 )
  64. return false;
  65. bool append = false;
  66. if( argc == 2 )
  67. append = dAtob( argv[1] );
  68. mAppend = append;
  69. mFilename = StringTable->insert( argv[0] );
  70. if( init() )
  71. {
  72. attach();
  73. return true;
  74. }
  75. return false;
  76. }
  77. //-----------------------------------------------------------------------------
  78. ConsoleLogger::~ConsoleLogger()
  79. {
  80. detach();
  81. }
  82. //-----------------------------------------------------------------------------
  83. bool ConsoleLogger::init()
  84. {
  85. if( smInitialized )
  86. return true;
  87. Con::addConsumer( ConsoleLogger::logCallback );
  88. smInitialized = true;
  89. return true;
  90. }
  91. //-----------------------------------------------------------------------------
  92. bool ConsoleLogger::attach()
  93. {
  94. if( mFilename == NULL )
  95. {
  96. Con::errorf( "ConsoleLogger failed to attach: no filename supplied." );
  97. return false;
  98. }
  99. // Check to see if this is initialized before using it
  100. if( !smInitialized )
  101. {
  102. if( !init() )
  103. {
  104. Con::errorf( "ConsoleLogger failed to initalize." );
  105. return false;
  106. }
  107. }
  108. if( mLogging )
  109. return false;
  110. // Open the filestream
  111. mStream.open( mFilename, ( mAppend ? FileStream::WriteAppend : FileStream::Write ) );
  112. // Add this to list of active loggers
  113. mActiveLoggers.push_back( this );
  114. mLogging = true;
  115. return true;
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool ConsoleLogger::detach()
  119. {
  120. // Make sure this is valid before messing with it
  121. if( !smInitialized )
  122. {
  123. if( !init() )
  124. {
  125. return false;
  126. }
  127. }
  128. if( !mLogging )
  129. return false;
  130. // Close filestream
  131. mStream.close();
  132. // Remove this object from the list of active loggers
  133. for( int i = 0; i < mActiveLoggers.size(); i++ )
  134. {
  135. if( mActiveLoggers[i] == this )
  136. {
  137. mActiveLoggers.erase( i );
  138. mLogging = false;
  139. return true;
  140. }
  141. }
  142. return false; // If this happens, it's bad...
  143. }
  144. //-----------------------------------------------------------------------------
  145. void ConsoleLogger::logCallback( ConsoleLogEntry::Level level, const char *consoleLine )
  146. {
  147. ConsoleLogger *curr;
  148. // Loop through active consumers and send them the message
  149. for( int i = 0; i < mActiveLoggers.size(); i++ )
  150. {
  151. curr = mActiveLoggers[i];
  152. // If the log level is within the log threshhold, log it
  153. if( curr->mLevel <= level )
  154. curr->log( consoleLine );
  155. }
  156. }
  157. //-----------------------------------------------------------------------------
  158. void ConsoleLogger::log( const char *consoleLine )
  159. {
  160. // Check to see if this is intalized before using it
  161. if( !smInitialized )
  162. {
  163. if( !init() )
  164. {
  165. Con::errorf( "I don't know how this happened, but log called on this without it being initialized" );
  166. return;
  167. }
  168. }
  169. mStream.writeLine( (U8 *)consoleLine );
  170. }
  171. //-----------------------------------------------------------------------------