1 //===-- MICmnLog.h ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #pragma once 11 12 // Third party headers: 13 #include <map> 14 15 // In-house headers: 16 #include "MICmnBase.h" 17 #include "MIUtilSingletonBase.h" 18 #include "MIUtilString.h" 19 20 //++ 21 //============================================================================ 22 // Details: MI common code implementation class. Handle application trace 23 // activity logging. Medium objects derived from the Medium abstract 24 /// class are registered with this logger. The function Write is called 25 // by a client callee to log information. That information is given to 26 // registered relevant mediums. The medium file is registered during 27 // *this logs initialization so it will always have a file log for the 28 // application. 29 // Singleton class. 30 //-- 31 class CMICmnLog : public MI::ISingleton<CMICmnLog> { 32 friend MI::ISingleton<CMICmnLog>; 33 34 // Enumeration: 35 public: 36 //++ 37 // Description: Data given to the Logger can be of several types. The Logger 38 // can be 39 // set at levels of verbosity. Can determine how data is sent to 40 // one or 41 // mediums. 42 //-- 43 enum ELogVerbosity { // Descriptions of what 'may' occur, depends ultimately 44 // on the medium itself. See the medium. 45 eLogVerbosity_FnTrace = 0x00000004, // Debug function stack call tracing 46 eLogVerbosity_DbgOp = 0x00000008, // Send a string to the debugger for 47 // display (not implemented) 48 eLogVerbosity_ClientMsg = 0x00000010, // A client using MI can insert 49 // messages into the log (not 50 // implemented) 51 eLogVerbosity_Log = 0x00000020 // Send to only the Log file. 52 }; 53 54 // Class: 55 public: 56 //++ 57 // Description: Register a medium derived from this interface which will be 58 // called writing log trace data i.e. a file or a console. 59 // Medium objects registered are not owned by *this logger. 60 //-- 61 class IMedium { 62 public: 63 virtual bool Initialize() = 0; 64 virtual const CMIUtilString &GetName() const = 0; 65 virtual bool Write(const CMIUtilString &vData, 66 const ELogVerbosity veType) = 0; 67 virtual const CMIUtilString &GetError() const = 0; 68 virtual bool Shutdown() = 0; 69 70 // Not part of the interface, ignore 71 // AD: This virtual destructor seems to hit a bug in the stdlib 72 // where vector delete is incorrectly called. Workaround is 73 // to comment this out while I investigate. ~IMedium()74 /* dtor */ virtual ~IMedium() {} 75 }; 76 77 // Statics: 78 public: 79 static bool WriteLog(const CMIUtilString &vData); 80 81 // Methods: 82 public: 83 bool RegisterMedium(const IMedium &vrMedium); 84 bool UnregisterMedium(const IMedium &vrMedium); 85 bool Write(const CMIUtilString &vData, const ELogVerbosity veType); 86 bool SetEnabled(const bool vbYes); 87 bool GetEnabled() const; 88 89 // MI common object handling - duplicate of CMICmnBase functions, necessary 90 // for LINUX build 91 // Done to stop locking on object construction init circular dependency. 92 const CMIUtilString &GetErrorDescription() const; 93 void SetErrorDescription(const CMIUtilString &vrTxt) const; 94 void ClrErrorDescription() const; 95 96 // Overridden: 97 public: 98 // From MI::ISingleton 99 bool Initialize() override; 100 bool Shutdown() override; 101 102 // Methods: 103 private: 104 /* ctor */ CMICmnLog(); 105 /* ctor */ CMICmnLog(const CMICmnLog &); 106 void operator=(const CMICmnLog &); 107 108 // Overridden: 109 private: 110 // From CMICmnBase 111 /* dtor */ ~CMICmnLog() override; 112 113 // Typedef: 114 private: 115 typedef std::map<IMedium *, CMIUtilString> MapMediumToName_t; 116 typedef std::pair<IMedium *, CMIUtilString> MapPairMediumToName_t; 117 118 // Methods: 119 private: 120 bool HaveMediumAlready(const IMedium &vrMedium) const; 121 bool UnregisterMediumAll(); 122 123 // Attributes: 124 private: 125 bool m_bRecursiveDive; // True = yes recursive, false = no 126 MapMediumToName_t m_mapMediumToName; 127 bool m_bEnabled; // True = Logger enabled for writing to mediums, false = 128 // medium not written to 129 bool m_bInitializingATM; // True = Yes in process of initing *this logger, 130 // false = not initing 131 // 132 // MI common object handling - duplicate of CMICmnBase functions, necessary 133 // for LINUX build 134 bool m_bInitialized; // True = yes successfully initialized, false = no yet or 135 // failed 136 mutable CMIUtilString m_strMILastErrorDescription; 137 MIint m_clientUsageRefCnt; // Count of client using *this object so not 138 // shutdown() object to early 139 }; 140