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