bux API Reference 1.12.3
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
LogStream.cpp
Go to the documentation of this file.
1#include "LogStream.h"
2//---------------------------------------------------------------
3#include <format> // std::format()
4#include <ostream> // std::ostream
5#include <cinttypes> // imaxdiv()
6#include <cstring> // std::strcpy()
7
8#define STD_FORMAT_CHRONO_
9#ifndef STD_FORMAT_CHRONO_
10#include <ctime> // std::localtime(), std::strftime()
11#endif
12
13#ifdef __APPLE__
14// based on https://developer.apple.com/library/archive/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13
15#include <pthread.h>
16#define TID_ pthread_self()
17#elifdef __GNUC__
18#ifndef _GNU_SOURCE
19#define _GNU_SOURCE // or _BSD_SOURCE or _SVID_SOURCE
20#endif
21#include <unistd.h>
22#include <sys/syscall.h> // for SYS_xxx definitions
23#define TID_ syscall(SYS_gettid)
24#elifdef _WIN32
25#include <windows.h> // GetCurrentThreadId()
26
27#define TID_ GetCurrentThreadId()
28#else
29#include <thread> // std::this_thread::get_id()
30
31#define TID_ std::this_thread::get_id()
32#endif
33
34namespace bux {
35
36std::ostream &timestamp(std::ostream &out, T_LocalZone tz)
43{
44 typedef std::chrono::system_clock myclock;
45 static char YMDHMS[30];
46 static std::chrono::time_point<myclock,std::chrono::milliseconds> old_time;
47 static T_LocalZone old_tz{};
48 const auto cur_time = time_point_cast<std::chrono::milliseconds>(myclock::now());
49 if (cur_time != old_time || tz != old_tz)
50 {
51#ifdef STD_FORMAT_CHRONO_
52 constexpr const std::string_view TIMESTAMP_FMT = "{:%Y/%m/%d %H:%M:%S}";
53 std::string tm_str;
54 if (tz)
55 {
56#if LOCALZONE_IS_TIMEZONE
57 auto ltm = tz->to_local(cur_time);
58#else
59 auto sys_t = std::chrono::system_clock::to_time_t(cur_time);
60 std::chrono::local_time<std::chrono::milliseconds> ltm(cur_time.time_since_epoch() + std::chrono::seconds(localtime(&sys_t)->tm_gmtoff));
61#endif
62 tm_str = std::format(TIMESTAMP_FMT, ltm);
63 }
64 else
65 tm_str = std::format(TIMESTAMP_FMT, cur_time);
66
67 std::strcpy(YMDHMS, tm_str.c_str());
68#else
69 auto d = imaxdiv(cur_time.time_since_epoch().count(), 1000);
70 time_t t = d.quot;
71 std::sprintf(YMDHMS + std::strftime(YMDHMS, sizeof YMDHMS, "%Y/%m/%d %H:%M:%S", std::localtime(&t)), ".%03" PRIdMAX, d.rem);
72#endif
73 old_time = cur_time;
74 old_tz = tz;
75 }
76 return out <<YMDHMS;
77}
78
79std::ostream &logTrace(std::ostream &out, T_LocalZone tz)
86{
87 return timestamp(out,tz) <<" tid" <<TID_ <<' ';
88}
89
90} // namespace bux
#define TID_
Definition LogStream.cpp:31
THE common namespace of bux library.
Definition AtomiX.cpp:3
const std::chrono::time_zone * T_LocalZone
Definition XPlatform.h:18
std::ostream & timestamp(std::ostream &out, T_LocalZone tz)
Definition LogStream.cpp:36
std::ostream & logTrace(std::ostream &out, T_LocalZone tz)
Definition LogStream.cpp:79