[4] | 1 | /**
|
---|
| 2 | * @file logger.h
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief logger
|
---|
| 5 | */
|
---|
| 6 | #ifndef __logger_h__
|
---|
| 7 | #define __logger_h__
|
---|
| 8 |
|
---|
| 9 | #include "cf/exception.h"
|
---|
| 10 | #include "cf/task.h"
|
---|
| 11 | #include "cf/util.h"
|
---|
| 12 |
|
---|
| 13 | #include <stdio.h>
|
---|
| 14 |
|
---|
| 15 | #include <string>
|
---|
| 16 | #include <vector>
|
---|
| 17 |
|
---|
| 18 | namespace cf
|
---|
| 19 | {
|
---|
| 20 | /**
|
---|
| 21 | * singleton logger
|
---|
| 22 | */
|
---|
| 23 | class logger
|
---|
| 24 | {
|
---|
| 25 | private:
|
---|
| 26 | /* log file management */
|
---|
| 27 | std::string mPath;
|
---|
| 28 | cf::int32_t mLogLevel;
|
---|
| 29 | task::mutex mOpenMutex;
|
---|
| 30 | task::mutex mRenameMutex;
|
---|
| 31 |
|
---|
| 32 | typedef struct _LOG_CTX
|
---|
| 33 | {
|
---|
| 34 | cf::int32_t mOption;
|
---|
| 35 | struct
|
---|
| 36 | {
|
---|
| 37 | cf::int32_t mMaxLogSize; /**< mega-bytes */
|
---|
| 38 | cf::int32_t mCurrentDateTime; /**< yyyymmdd */
|
---|
| 39 | } mOptionParam;
|
---|
| 40 |
|
---|
| 41 | std::string mPrefix;
|
---|
| 42 | std::string mDescription;
|
---|
| 43 | std::string mFullPath;
|
---|
| 44 | FILE * mFP;
|
---|
| 45 | } LOG_CTX;
|
---|
| 46 |
|
---|
| 47 | /* level == index-of-vector */
|
---|
| 48 | std::vector<LOG_CTX> mLogPool;
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * constructor
|
---|
| 52 | * @throw cf::exception
|
---|
| 53 | */
|
---|
| 54 | logger()
|
---|
| 55 | throw (cf::exception);
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * destructor
|
---|
| 59 | */
|
---|
| 60 | ~logger();
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * open log file
|
---|
| 64 | * @param level level to open file
|
---|
| 65 | * @throw cf::exception
|
---|
| 66 | */
|
---|
| 67 | cf::void_t open(const cf::int32_t level)
|
---|
| 68 | throw (cf::exception);
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * rename for rotation
|
---|
| 72 | * @param level log level
|
---|
| 73 | * @throw cf::exception
|
---|
| 74 | */
|
---|
| 75 | cf::void_t rename(const cf::int32_t level)
|
---|
| 76 | throw (cf::exception);
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * overwrite newfp to fp for fullpath
|
---|
| 80 | * @param prefix prefix of fileprefix
|
---|
| 81 | * @param option option
|
---|
| 82 | * @param newfp new fp
|
---|
| 83 | */
|
---|
| 84 | cf::void_t replace(const std::string & prefix,
|
---|
| 85 | const cf::int32_t option,
|
---|
| 86 | FILE * newfp);
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * date rotation
|
---|
| 90 | * @param level log level
|
---|
| 91 | * @param dt datetime
|
---|
| 92 | * @throw cf::exception
|
---|
| 93 | */
|
---|
| 94 | cf::void_t rotateByDate(const cf::int32_t level,
|
---|
| 95 | const util::datetime & dt)
|
---|
| 96 | throw (cf::exception);
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * size rotation
|
---|
| 100 | * @param level log level
|
---|
| 101 | * @throw cf::exception
|
---|
| 102 | */
|
---|
| 103 | cf::void_t rotateBySize(const cf::int32_t level)
|
---|
| 104 | throw (cf::exception);
|
---|
| 105 |
|
---|
| 106 | public:
|
---|
| 107 | /**
|
---|
| 108 | * log option
|
---|
| 109 | */
|
---|
| 110 | enum _LOG_OPTION
|
---|
| 111 | {
|
---|
| 112 | NO_OPTION = 0x00000000, /**< no option */
|
---|
| 113 | FORCED = 0x00000001, /**< forced write */
|
---|
| 114 | DAILY = 0x00000010, /**< daily rotation */
|
---|
| 115 | SIZE = 0x00000020, /**< size rotation */
|
---|
| 116 | SECURE = 0x00000100 /**< encrypt message */
|
---|
| 117 | };
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * get instance
|
---|
| 121 | * @return global instance
|
---|
| 122 | */
|
---|
| 123 | static logger * getInstance();
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * initialize
|
---|
| 127 | * @param path directory path for log
|
---|
| 128 | * @throw cf::exception
|
---|
| 129 | */
|
---|
| 130 | cf::void_t init(const std::string & path);
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * add log level
|
---|
| 134 | * @param prefix file prefix
|
---|
| 135 | * @param level log level
|
---|
| 136 | * @param description description for log level
|
---|
| 137 | * @param option option(i.e., forced, rotation)
|
---|
| 138 | * @param rotationSize rotation parameter(i.e., size)
|
---|
| 139 | * @throw cf::exception
|
---|
| 140 | * @see _LOG_OPTION
|
---|
| 141 | * @remarks start from 1
|
---|
| 142 | */
|
---|
| 143 | cf::void_t add(const std::string & prefix,
|
---|
| 144 | const cf::int32_t level,
|
---|
| 145 | const std::string & description,
|
---|
| 146 | const cf::int32_t option,
|
---|
| 147 | const cf::int32_t rotationSize = 0)
|
---|
| 148 | throw (cf::exception);
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * get log level
|
---|
| 152 | * @return log level
|
---|
| 153 | */
|
---|
| 154 | cf::int32_t getLevel();
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * set log level
|
---|
| 158 | * @param level log level
|
---|
| 159 | */
|
---|
| 160 | cf::void_t setLevel(const cf::int32_t level);
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * get file path
|
---|
| 164 | * @return file path
|
---|
| 165 | * @param level log level
|
---|
| 166 | * @throw cf::exception
|
---|
| 167 | */
|
---|
| 168 | std::string getPath(const cf::int32_t level) const
|
---|
| 169 | throw (cf::exception);
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * is log level registered ?
|
---|
| 173 | * @return true; false
|
---|
| 174 | * @param level log level
|
---|
| 175 | */
|
---|
| 176 | cf::bool_t isRegistered(const cf::int32_t level) const;
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * is log level enabled ?
|
---|
| 180 | * @return true; false
|
---|
| 181 | * @param level log level
|
---|
| 182 | */
|
---|
| 183 | cf::bool_t isEnabled(const cf::int32_t level) const;
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * write
|
---|
| 187 | * @param level level
|
---|
| 188 | * @param msg message
|
---|
| 189 | */
|
---|
| 190 | cf::void_t write(const cf::int32_t level,
|
---|
| 191 | const std::string & msg);
|
---|
| 192 | };
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | #endif // #ifndef __logger_h__
|
---|