1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| namespace tlog { class tlog_category_interface; class tlog_ctx_interface; }; typedef tlog::tlog_ctx_interface* LPTLOGCTX; typedef tlog::tlog_category_interface* LPLOGCATEGORYINST; LPTLOGCTX tlog_init_from_file(const char*); LPLOGCATEGORYINST tlog_get_category(LPTLOGCTX, const char*); void tlog_info(LPLOGCATEGORYINST, size_t, size_t, const char*); int tlog_fini_ctx(LPTLOGCTX*);
namespace tlog{ class tlog_category_interface { public: ~tlog_category_interface(){} virtual void exec(const char*) = 0; virtual int release() = 0; }; class tlog_ctx_interface { public: ~tlog_ctx_interface(){} virtual tlog_category_interface* get_category(const char*) = 0; virtual int release(); }; enum EN_DRVICE_TYPE { en_device_file, en_device_net, }; struct stDevice { EN_DRVICE_TYPE _enType; stDevice(EN_DRVICE_TYPE enType) :_enType(enType) {} virtual EN_DRVICE_TYPE get_type() { return _enType; } virtual bool init(pugi::xml_node node) = 0; virtual int release() = 0; virtual void exec(const char* pszData) = 0; }; struct stDeviceFile : public stDevice { std::string _pattern; uint32_t _bufferSize; std::string _data; std::ofstream _out; stDeviceFile() : stDevice(en_device_file) {} virtual bool init(pugi::xml_node node) { } virtual int release() { } virtual void exec(const char* pStr) { _data.append(pStr); _data.append("\n"); if (_data.size() >= _bufferSize) { _out << _data; _data.clear(); } } }; struct stDeviceNet : public stDevice { char _protocol[32]; char _ip[64]; char _port[8]; int _sock_c; stDeviceNet() : stDevice(en_device_net) {} virtual bool init(pugi::xml_node node) { } virtual int release() { } virtual void exec(const char* pStr) { sockaddr_in ser; bzero(&ser, sizeof(ser)); ser.sin_family = AF_INET; ser.sin_port = htons(atoi(_port)); ser.sin_addr.s_addr = inet_addr(_ip); int nlen = sizeof(ser); sendto(_sock_c, pStr, strlen(pStr) + 1, 0, (sockaddr*)&ser, nlen); } }; class tlog_category : public tlog_category_interface { private: std::string _name; std::vector<stDevice*> _vecDevice; public: bool init(pugi::xml_node node) { } virtual int release() { } virtual void exec(const char* pstr) { } }; class tlog_ctx : public tlog_ctx_interface { private: std::unordered_map<std::string, tlog_category*> _mapCate; public: bool init(const char* pszPath) { } virtual int release() { } virtual tlog_category_interface* get_category(const char* name) { return _mapCate[name]; } }; };
LPTLOGCTX tlog_init_from_file(const char* pszPath) { tlog_ctx* pCtx = new tlog_ctx; pCtx->init(pszPath); return pCtx; } LPLOGCATEGORYINST tlog_get_category(LPTLOGCTX pstCtx, const char* pszName) { if (pstCtx) { return pstCtx->get_category(pszName); } return NULL; } void tlog_info(LPLOGCATEGORYINST pCat, size_t id, size_t, cls, const char* pszFmt, ...) { if (!pCat) return; va_list vArg; va_start(vArg, pszFmt); char buffer[MAX_SIZE] = {0}; vsnprintf(buffer, MAX_SIZE, pszFmt, vArg); pCat->exec(buffer); va_end(vArg); } int tlog_fini_ctx(LPTLOGCTX* ppstCtx) { if (ppstCtx && *ppstCtx) { (*ppstCtx)->release(); *ppstCtx = NULL; return 0; } return -1; }
|