consoleLogger.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef _CONSOLE_LOGGER_H_
  23. #define _CONSOLE_LOGGER_H_
  24. #ifndef _SIMOBJECT_H_
  25. #include "console/simObject.h"
  26. #endif
  27. #ifndef _CONSOLE_H_
  28. #include "console/console.h"
  29. #endif
  30. #ifndef _FILESTREAM_H_
  31. #include "core/stream/fileStream.h"
  32. #endif
  33. /// @ingroup console_system Console System
  34. /// @{
  35. typedef ConsoleLogEntry::Level LogLevel;
  36. DefineEnumType( LogLevel );
  37. /// A class designed to be used as a console consumer and log
  38. /// the data it receives to a file.
  39. class ConsoleLogger : public SimObject
  40. {
  41. typedef SimObject Parent;
  42. private:
  43. bool mLogging; ///< True if it is currently consuming and logging
  44. FileStream mStream; ///< File stream this object writes to
  45. static bool smInitialized; ///< This is for use with the default constructor
  46. bool mAppend; ///< If false, it will clear the file before logging to it.
  47. StringTableEntry mFilename; ///< The file name to log to.
  48. /// List of active ConsoleLoggers to send log messages to
  49. static Vector<ConsoleLogger *> mActiveLoggers;
  50. /// The log function called by the consumer callback
  51. /// @param consoleLine Line of text to log
  52. void log( const char *consoleLine );
  53. /// Utility function, sets up the object (for script interface) returns true if successful
  54. bool init();
  55. public:
  56. // @name Public console variables
  57. /// @{
  58. ConsoleLogEntry::Level mLevel; ///< The level of log messages to log
  59. /// @}
  60. DECLARE_CONOBJECT( ConsoleLogger );
  61. static void initPersistFields();
  62. /// Console constructor
  63. ///
  64. /// @code
  65. /// // Example script constructor usage.
  66. /// %obj = new ConsoleLogger( objName, logFileName, [append = false] );
  67. /// @endcode
  68. bool processArguments( S32 argc, ConsoleValue *argv );
  69. /// Default constructor, make sure to initalize
  70. ConsoleLogger();
  71. /// Constructor
  72. /// @param fileName File name to log to
  73. /// @param append If false, it will clear the file, then start logging, else it will append
  74. ConsoleLogger( const char *fileName, bool append = false );
  75. /// Destructor
  76. ~ConsoleLogger();
  77. /// Attach to the console and begin logging
  78. ///
  79. /// Returns true if the action is successful
  80. bool attach();
  81. /// Detach from the console and stop logging
  82. ///
  83. /// Returns true if the action is successful
  84. bool detach();
  85. /// Sets the level of console messages to log.
  86. ///
  87. /// @param level Log level. Only items of the specified level or
  88. /// lower are logged.
  89. /// @see ConsoleLogEntry::Level
  90. void setLogLevel( ConsoleLogEntry::Level level );
  91. /// Returns the level of console messages to log
  92. ConsoleLogEntry::Level getLogLevel() const;
  93. /// The callback for the console consumer
  94. ///
  95. /// @note This is a global callback, not executed per-instance.
  96. /// @see Con::addConsumer
  97. static void logCallback( U32 level, const char *consoleLine );
  98. };
  99. /// @}
  100. #endif // _CONSOLE_LOGGER_H_