Repo-Init
 
Loading...
Searching...
No Matches
spdlog::sinks::loki_api_sink< Mutex > Class Template Reference

#include <Loki.hpp>

Inheritance diagram for spdlog::sinks::loki_api_sink< Mutex >:
Collaboration diagram for spdlog::sinks::loki_api_sink< Mutex >:

Classes

struct  logInfo_t
 

Public Member Functions

 loki_api_sink (const std::string &lokiAddress)
 
 ~loki_api_sink ()
 

Protected Member Functions

void sink_it_ (const details::log_msg &msg) override
 
void flush_ () override
 

Private Attributes

bool _lokiAvailable {false}
 
std::unique_ptr< HTTP_connHandler
 
std::string _basicInformation
 
std::vector< struct logInfo_t_internalLogBuffer
 

Detailed Description

template<typename Mutex>
class spdlog::sinks::loki_api_sink< Mutex >

A custom sink for spdlog that sends log messages to a Loki server.

The loki_api_sink class is a custom sink for spdlog that sends log messages to a Loki server. It provides functionality to send log messages to a specified Loki server address. The log messages are sent using HTTP requests.

Template Parameters
MutexThe type of mutex to use for thread-safety.

Definition at line 24 of file Loki.hpp.

Constructor & Destructor Documentation

◆ loki_api_sink()

template<typename Mutex >
spdlog::sinks::loki_api_sink< Mutex >::loki_api_sink ( const std::string & lokiAddress)
explicit

Constructs a loki_api_sink object with the specified Loki server address.

Parameters
lokiAddressThe address of the Loki server to send log messages to.

Definition at line 19 of file Loki.cpp.

20 {
21 if (lokiAddress.empty())
22 {
23 return;
24 }
25
26 _connHandler = std::make_unique<HTTP>(lokiAddress);
27
28 // Pre-allocate buffers
29 _internalLogBuffer.push_back({"debug", std::vector<std::pair<std::string, std::string>>()});
30 _internalLogBuffer.push_back({"info", std::vector<std::pair<std::string, std::string>>()});
31 _internalLogBuffer.push_back({"warn", std::vector<std::pair<std::string, std::string>>()});
32 _internalLogBuffer.push_back({"error", std::vector<std::pair<std::string, std::string>>()});
33 _internalLogBuffer.push_back({"critical", std::vector<std::pair<std::string, std::string>>()});
34
35 // Prepare information (Loki limits maximum number of labels with 15)
37 _basicInformation += std::string(R"("release_version":"v)") + PROJECT_FULL_REVISION + "\",";
38 _basicInformation += std::string(R"("release_date":")") + PROJECT_BUILD_DATE + " " + PROJECT_BUILD_TIME + "\",";
39 _basicInformation += std::string(R"("compiler_name":")") + COMPILER_NAME + "\",";
40 _basicInformation += std::string(R"("compiler_version":")") + COMPILER_VERSION + "\",";
41 _basicInformation += std::string(R"("build":")") + BUILD_TYPE + "\",";
42
43 // Parse hostname
44 std::array<char, BUFSIZ> hostBuffer{};
45 gethostname(hostBuffer.data(), BUFSIZ);
46 _basicInformation += std::string(R"("hostname":")") + hostBuffer.data() + "\",";
47
48 // Parse CPU information
49 const std::string cpuInfoPath = "/proc/cpuinfo";
50 std::string word;
51
52 findFromFile(cpuInfoPath, "^siblings", word);
53 _basicInformation += std::string(R"("cpu_threadcount":")") + word + "\",";
54 findFromFile(cpuInfoPath, "^(cpu cores)", word);
55 _basicInformation += std::string(R"("cpu_corecount":")") + word + "\",";
56 findFromFile(cpuInfoPath, "^(model name)", word);
57 _basicInformation += std::string(R"("cpu_model":")") + word + "\",";
58 findFromFile(cpuInfoPath, "^vendor_id", word);
59 _basicInformation += std::string(R"("cpu_vendorid":")") + word + "\",";
60
61 _lokiAvailable = true;
62 }
std::vector< std::string > findFromFile(const std::string &filePath, const std::string &pattern, std::string &lastWord)
std::unique_ptr< HTTP > _connHandler
Definition Loki.hpp:59
std::vector< struct logInfo_t > _internalLogBuffer
Definition Loki.hpp:66
std::string _basicInformation
Definition Loki.hpp:60
Here is the call graph for this function:

◆ ~loki_api_sink()

template<typename Mutex >
spdlog::sinks::loki_api_sink< Mutex >::~loki_api_sink ( )
default

Destroys the loki_api_sink object.

Member Function Documentation

◆ flush_()

template<typename Mutex >
void spdlog::sinks::loki_api_sink< Mutex >::flush_ ( )
overrideprotected

Flushes any buffered log messages.

This function is called by spdlog to flush any buffered log messages. It is overridden from the base_sink class.

Definition at line 81 of file Loki.cpp.

82 {
83 bool flag = false;
84
85 // Prepare JSON
86 std::ostringstream sStream;
87
88 sStream << "{\"streams\":[";
89 for (auto &entry : _internalLogBuffer)
90 {
91 if (entry.logs.empty())
92 {
93 continue;
94 }
95
96 if (flag)
97 {
98 sStream << ",";
99 }
100 flag = true;
101 sStream << "{\"stream\":{" << _basicInformation + R"("level":")" << entry.level << R"("},"values":[)";
102
103 bool subflag = false;
104 for (const auto &subentry : entry.logs)
105 {
106 if (subflag)
107 {
108 sStream << ",";
109 }
110 subflag = true;
111 sStream << "[\"" << subentry.first << "\",\"" << subentry.second << "\"]";
112 }
113 sStream << "]}";
114
115 entry.logs.clear();
116 }
117 sStream << "]}";
118
119 if (flag)
120 {
121 // Send request
122 std::string recvData;
123 HttpStatus::Code replyCode = HttpStatus::Code::xxx_max;
124 _connHandler->sendPOSTRequest("/loki/api/v1/push", sStream.str(), recvData, replyCode);
125 }
126 }

◆ sink_it_()

template<typename Mutex >
void spdlog::sinks::loki_api_sink< Mutex >::sink_it_ ( const details::log_msg & msg)
overrideprotected

Sends the log message to the Loki server.

This function is called by spdlog to send the log message to the Loki server. It is overridden from the base_sink class.

Parameters
msgThe log message to be sent.

Definition at line 66 of file Loki.cpp.

67 {
68 if (!_lokiAvailable)
69 {
70 return;
71 }
72
73 if (msg.level >= spdlog::level::debug && msg.level <= spdlog::level::critical)
74 {
75 _internalLogBuffer[static_cast<size_t>(msg.level) - 1].logs.push_back(
76 {std::to_string(msg.time.time_since_epoch().count()),
77 std::string(msg.payload.data(), msg.payload.size())});
78 }
79 }

Member Data Documentation

◆ _basicInformation

template<typename Mutex >
std::string spdlog::sinks::loki_api_sink< Mutex >::_basicInformation
private

Definition at line 60 of file Loki.hpp.

◆ _connHandler

template<typename Mutex >
std::unique_ptr<HTTP> spdlog::sinks::loki_api_sink< Mutex >::_connHandler
private

Definition at line 59 of file Loki.hpp.

◆ _internalLogBuffer

template<typename Mutex >
std::vector<struct logInfo_t> spdlog::sinks::loki_api_sink< Mutex >::_internalLogBuffer
private

Definition at line 66 of file Loki.hpp.

◆ _lokiAvailable

template<typename Mutex >
bool spdlog::sinks::loki_api_sink< Mutex >::_lokiAvailable {false}
private

Definition at line 58 of file Loki.hpp.

58{false};

The documentation for this class was generated from the following files: