LogSystem beta 1.4
This is a logging system project
 
载入中...
搜索中...
未找到
codec.hpp
浏览该文件的文档.
1
3#pragma once
4#include <jsoncpp/json/json.h>
5#include <iostream>
6#include <thread>
7#include <string>
8#include <vector>
9#include <cstring>
10#include <jsoncpp/json/json.h>
11#include "../logs/message.hpp"
12
13namespace Xulog
14{
18 {
19 std::string msg_mod;
20 std::string unformatted_msg;
22 };
25 class Codec
26 {
27 public:
31 static Json::Value toJson(const LogMsg &msg)
32 {
33 Json::Value json;
34 json["format_msg"] = msgToJson(msg);
35 json["msg_mod"] = "format";
36 return json;
37 }
41 static Json::Value toJson(const std::string &msg)
42 {
43 Json::Value json;
44 json["unformatted_msg"] = msg;
45 json["msg_mod"] = "unformatted";
46 return json;
47 }
51 static DeliverMsg fromJson(const Json::Value &json)
52 {
53 DeliverMsg dmsg;
54 dmsg.msg_mod = json["msg_mod"].asString();
55 if (dmsg.msg_mod == "format")
56 {
57 dmsg.format_msg = msgFromJson(json["format_msg"]);
58 }
59 else
60 dmsg.unformatted_msg = json["unformatted_msg"].asString();
61 return dmsg;
62 }
63
64 private:
68 static Json::Value msgToJson(const LogMsg &msg)
69 {
70 Json::Value json;
71 json["ctime"] = static_cast<Json::Int64>(msg._ctime);
72 json["line"] = msg._line;
73 json["tid"] = std::to_string(*(unsigned long int *)(&msg._tid));
74 json["level"] = LogLevel::toString(msg._level);
75 json["file"] = msg._file;
76 json["logger"] = msg._logger;
77 json["payload"] = msg._payload;
78 return json;
79 }
83 static LogMsg msgFromJson(const Json::Value &json)
84 {
85 LogMsg msg;
86 msg._ctime = json["ctime"].asInt64();
87 msg._line = json["line"].asUInt();
88 msg._tid = std::thread::id(std::stoul(json["tid"].asString()));
89 msg._level = LogLevel::fromString(json["level"].asString());
90 msg._file = json["file"].asString();
91 msg._logger = json["logger"].asString();
92 msg._payload = json["payload"].asString();
93 return msg;
94 }
95 };
96}
@classCodec
Definition codec.hpp:26
static DeliverMsg fromJson(const Json::Value &json)
将Json信息反序列化为传递消息
Definition codec.hpp:51
static Json::Value toJson(const std::string &msg)
将非结构化信息序列化为Json格式
Definition codec.hpp:41
static LogMsg msgFromJson(const Json::Value &json)
将Json信息反序列化为传递消息
Definition codec.hpp:83
static Json::Value toJson(const LogMsg &msg)
将结构化信息序列化为Json格式
Definition codec.hpp:31
static Json::Value msgToJson(const LogMsg &msg)
将结构化信息序列化为Json格式
Definition codec.hpp:68
static LogLevel::value fromString(const std::string &level)
从字符串转换成日志等级
Definition level.hpp:73
static const char * toString(LogLevel::value level)
将日志等级转换为对应的字符串
Definition level.hpp:44
Definition buffer.hpp:12
传递的消息
Definition codec.hpp:18
std::string unformatted_msg
非结构化消息
Definition codec.hpp:20
std::string msg_mod
消息模式
Definition codec.hpp:19
LogMsg format_msg
结构化信息
Definition codec.hpp:21
日志消息结构体
Definition message.hpp:24
size_t _line
行号
Definition message.hpp:26
std::string _payload
有效载荷数据
Definition message.hpp:31
time_t _ctime
日志产生的时间戳
Definition message.hpp:25
std::string _file
源文件名称
Definition message.hpp:29
std::string _logger
日志器
Definition message.hpp:30
std::thread::id _tid
线程ID
Definition message.hpp:27
LogLevel::value _level
日志等级
Definition message.hpp:28