consoleLogger.h 4.2 KB

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