API
 
Loading...
Searching...
No Matches
logStdFormat.hpp
Go to the documentation of this file.
1/** \file logStdFormat.hpp
2 * \brief Standard formating of log entries for readable output.
3 * \author Jared R. Males (jaredmales@gmail.com)
4 *
5 * \ingroup flatlogs_files
6 *
7 * History:
8 * - 2017-12-24 created by JRM
9 * - 2018-08-18 moved to flatlogs
10 */
11
12#ifndef flatlogs_logStdFormat_hpp
13#define flatlogs_logStdFormat_hpp
14
15#include "flatbuffers/flatbuffers.h"
16#include "flatbuffers/minireflect.h"
17
18#include "logHeader.hpp"
19#include "logPriority.hpp"
20
21namespace flatlogs
22{
23
24/// Function for generate a JSON-formatted text representation of a flatlog record
25/**
26 *
27 * \ingroup logformat
28 */
29template<typename logT, typename iosT>
30iosT & jsonFormat( iosT & ios, ///< [out] the iostream to output the log too
31 bufferPtrT & logBuffer, ///< [in] the binary log buffer to output
32 const std::string & eventCodeName,
33 const uint8_t * binarySchema,
34 const unsigned int binarySchemaLength
35 )
36{
37 logPrioT prio;
38 eventCodeT ec;
39 timespecX ts;
40 msgLenT len;
41
42 logHeader::extractBasicLog( prio, ec, ts, len, logBuffer);
43
44 ios << "{";
45 ios << "\"ts\": \"" << ts.ISO8601DateTimeStrX() << "\", ";
46 ios << "\"prio\": \"" << priorityString(prio) << "\", ";
47 ios << "\"ec\": \"" << eventCodeName << "\", ";
48 ios << "\"msg\": " << logT::msgJSON(logHeader::messageBuffer(logBuffer), len, binarySchema, binarySchemaLength);
49 ios << "}";
50 return ios;
51}
52
53
54/// Worker function that formats a log into the standard text representation.
55/**
56 *
57 * \ingroup logformat
58 */
59template<typename logT, typename iosT>
60iosT & stdFormat( iosT & ios, ///< [out] the iostream to output the log too
61 bufferPtrT & logBuffer ///< [in] the binary log buffer to output
62 )
63{
64 logPrioT prio;
65 eventCodeT ec;
66 timespecX ts;
67 msgLenT len;
68
69 logHeader::extractBasicLog( prio, ec, ts, len, logBuffer);
70
71 ios << ts.ISO8601DateTimeStrX() << " " << priorityString(prio) << " " << logT::msgString(logHeader::messageBuffer(logBuffer) , len);
72
73 return ios;
74}
75
76/// Worker function that formats a log into the standard text representation with short timespec.
77/**
78 *
79 * \ingroup logformat
80 */
81template<typename logT, typename iosT>
82iosT & stdShortFormat( iosT & ios, ///< [out] the iostream to output the log to
83 const std::string & appName,
84 bufferPtrT & logBuffer ///< [in] the binary log buffer to output
85 )
86{
87 logPrioT prio;
88 eventCodeT ec;
89 timespecX ts;
90 msgLenT len;
91
92 logHeader::extractBasicLog( prio, ec, ts, len, logBuffer);
93
94 std::string outApp;
95
96 if(appName.size() > 15)
97 {
98 outApp.resize(17, ' ');
99 outApp[16] = ':';
100 for(int n=15; n >= 0; --n)
101 {
102 if( 15-n + 1 > appName.size()) break;
103 outApp[n] = appName[appName.size()-1 - (15-n)];
104 }
105 }
106 else
107 {
108 outApp = appName;
109 outApp += ":";
110 outApp += std::string( 15-appName.size(), ' ');
111 }
112
113
114 ios << outApp << " " << ts.secondStrX() << " " << priorityString(prio) << " " << logT::msgString(logHeader::messageBuffer(logBuffer) , len);
115
116 return ios;
117}
118
119/// Worker function that formats a log into the standard text representation.
120/**
121 *
122 * \ingroup logformat
123 */
124template<typename logT, typename iosT>
125iosT & minFormat( iosT & ios, ///< [out] the iostream to output the log too
126 bufferPtrT & logBuffer ///< [in] the binary log buffer to output
127 )
128{
129 logPrioT prio;
130 eventCodeT ec;
131 timespecX ts;
132 msgLenT len;
133
134 logHeader::extractBasicLog( prio, ec, ts, len, logBuffer);
135
136 ios << priorityString(prio) << " " << logT::msgString(logHeader::messageBuffer(logBuffer) , len);
137
138 return ios;
139}
140
141} //namespace flatlogs
142
143#endif //flatlogs_logStdFormat_hpp
uint16_t eventCodeT
The type of an event code (16-bit unsigned int).
Definition logDefs.hpp:40
msgLen2T msgLenT
The type used to refer to the message length, regardless of length.
Definition logDefs.hpp:69
int8_t logPrioT
The type of the log priority code.
Definition logDefs.hpp:21
std::string priorityString(logPrioT &prio)
Get the string representation of a log priority.
static void * messageBuffer(bufferPtrT &logBuffer)
Get the message buffer address.
std::shared_ptr< char > bufferPtrT
The log entry buffer smart pointer.
Definition logHeader.hpp:58
static int extractBasicLog(logPrioT &lvl, eventCodeT &ec, timespecX &ts, msgLenT &len, bufferPtrT &logBuffer)
Extract the basic details of a log entry.
iosT & stdFormat(iosT &ios, bufferPtrT &logBuffer)
Worker function that formats a log into the standard text representation.
iosT & minFormat(iosT &ios, bufferPtrT &logBuffer)
Worker function that formats a log into the standard text representation.
iosT & stdShortFormat(iosT &ios, const std::string &appName, bufferPtrT &logBuffer)
Worker function that formats a log into the standard text representation with short timespec.
iosT & jsonFormat(iosT &ios, bufferPtrT &logBuffer, const std::string &eventCodeName, const uint8_t *binarySchema, const unsigned int binarySchemaLength)
Function for generate a JSON-formatted text representation of a flatlog record.
The flatlogs buffer header format.
The MagAO-X logger log priority levels.
A fixed-width timespec structure.
Definition timespecX.hpp:35
std::string secondStrX()
Get a date-time string with just the second for timespecX.
std::string ISO8601DateTimeStrX()
Get a date-time string in ISO 8601 format for timespecX.