-
Notifications
You must be signed in to change notification settings - Fork 3
/
zh_log.h
4488 lines (4034 loc) · 142 KB
/
zh_log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
* @Author: ZhangHao
* @Date: 2020-12-07 14:07:54
* @LastEditTime: 2020-12-08 17:04:54
* @LastEditors: ZhangHao
* @Description: 日志类,仅需本头文件即可
* @FilePath: /edge_ai_frame/business/include/zh_log.h
* @954217436@qq.com
*/
#pragma once
#ifndef _FN_LOG_FILE_H_
#define _FN_LOG_FILE_H_
#include <cstddef>
#include <cstring>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <array>
#include <mutex>
#include <thread>
#include <functional>
#include <regex>
#include <atomic>
#include <cmath>
#include <cfloat>
#include <list>
#include <deque>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <memory>
#include <atomic>
#define TRACE LogTrace()
#define DEBUG LogDebug()
#define INFO LogInfo()
#define WARN LogWarn()
#define ERROR LogError()
#define ALARM LogAlarm()
#define FETAL LogFatal()
#ifdef WIN32
#ifndef KEEP_INPUT_QUICK_EDIT
#define KEEP_INPUT_QUICK_EDIT false
#endif
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <Windows.h>
#include <io.h>
#include <shlwapi.h>
#include <process.h>
#pragma comment(lib, "shlwapi")
#pragma comment(lib, "User32.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
#else
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/syscall.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#
#endif
#ifdef __APPLE__
#include "TargetConditionals.h"
#include <dispatch/dispatch.h>
#if !TARGET_OS_IPHONE
#define NFLOG_HAVE_LIBPROC
#include <libproc.h>
#endif
#endif
namespace FNLog
{
static const int CHUNK_SIZE = 128;
class FileHandler
{
public:
inline FileHandler();
inline ~FileHandler();
inline bool is_open();
inline long open(const char* path, const char* mod, struct stat& file_stat);
inline void close();
inline void write(const char* data, size_t len);
inline void flush();
inline std::string read_line();
inline std::string read_content();
static inline bool is_dir(const std::string & path);
static inline bool is_file(const std::string & path);
static inline bool create_dir(const std::string& path);
static inline std::string process_id();
static inline std::string process_name();
static inline bool remove_file(const std::string& path);
static inline struct tm time_to_tm(time_t t);
static inline bool rollback(const std::string& path, int depth, int max_depth);
public:
char chunk_1_[128];
FILE* file_;
};
long FileHandler::open(const char* path, const char* mod, struct stat& file_stat)
{
if (file_ != nullptr)
{
fclose(file_);
file_ = nullptr;
}
file_ = fopen(path, mod);
if (file_)
{
if (fstat(fileno(file_), &file_stat) != 0)
{
fclose(file_);
file_ = nullptr;
return -1;
}
return file_stat.st_size;
}
return -2;
}
void FileHandler::close()
{
if (file_ != nullptr)
{
#if !defined(__APPLE__) && !defined(WIN32)
if (file_ != nullptr)
{
int fd = fileno(file_);
fsync(fd);
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
fsync(fd);
}
#endif
fclose(file_);
file_ = nullptr;
}
}
FileHandler::FileHandler()
{
file_ = nullptr;
}
FileHandler::~FileHandler()
{
close();
}
bool FileHandler::is_open()
{
return file_ != nullptr;
}
void FileHandler::write(const char* data, size_t len)
{
if (file_ && len > 0)
{
if (fwrite(data, 1, len, file_) != len)
{
close();
}
}
}
void FileHandler::flush()
{
if (file_)
{
fflush(file_);
}
}
std::string FileHandler::read_line()
{
char buf[500] = { 0 };
if (file_ && fgets(buf, 500, file_) != nullptr)
{
return std::string(buf);
}
return std::string();
}
std::string FileHandler::read_content()
{
std::string content;
if (!file_)
{
return content;
}
char buf[BUFSIZ];
size_t ret = 0;
do
{
ret = fread(buf, sizeof(char), BUFSIZ, file_);
content.append(buf, ret);
} while (ret == BUFSIZ);
return content;
}
bool FileHandler::is_dir(const std::string& path)
{
#ifdef WIN32
return PathIsDirectoryA(path.c_str()) ? true : false;
#else
DIR* pdir = opendir(path.c_str());
if (pdir == nullptr)
{
return false;
}
else
{
closedir(pdir);
pdir = nullptr;
return true;
}
#endif
}
bool FileHandler::is_file(const std::string& path)
{
#ifdef WIN32
return ::_access(path.c_str(), 0) == 0;
#else
return ::access(path.c_str(), F_OK) == 0;
#endif
}
bool FileHandler::create_dir(const std::string& path)
{
if (path.length() == 0)
{
return true;
}
std::string sub;
std::string::size_type pos = path.find('/');
while (pos != std::string::npos)
{
std::string cur = path.substr(0, pos - 0);
if (cur.length() > 0 && !is_dir(cur))
{
bool ret = false;
#ifdef WIN32
ret = CreateDirectoryA(cur.c_str(), nullptr) ? true : false;
#else
ret = (mkdir(cur.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0);
#endif
if (!ret)
{
return false;
}
}
pos = path.find('/', pos + 1);
}
return true;
}
std::string FileHandler::process_id()
{
std::string pid = "0";
char buf[260] = { 0 };
#ifdef WIN32
DWORD winPID = GetCurrentProcessId();
sprintf(buf, "%06u", winPID);
pid = buf;
#else
sprintf(buf, "%06d", getpid());
pid = buf;
#endif
return pid;
}
std::string FileHandler::process_name()
{
std::string name = "process";
char buf[260] = { 0 };
#ifdef WIN32
if (GetModuleFileNameA(nullptr, buf, 259) > 0)
{
name = buf;
}
std::string::size_type pos = name.rfind("\\");
if (pos != std::string::npos)
{
name = name.substr(pos + 1, std::string::npos);
}
pos = name.rfind(".");
if (pos != std::string::npos)
{
name = name.substr(0, pos - 0);
}
#elif defined(__APPLE__)
proc_name(getpid(), buf, 260);
name = buf;
return name;;
#else
sprintf(buf, "/proc/%d/cmdline", (int)getpid());
FileHandler i;
struct stat file_stat;
i.open(buf, "rb", file_stat);
if (!i.is_open())
{
return name;
}
name = i.read_line();
i.close();
std::string::size_type pos = name.rfind("/");
if (pos != std::string::npos)
{
name = name.substr(pos + 1, std::string::npos);
}
#endif
return name;
}
bool FileHandler::remove_file(const std::string & path)
{
return ::remove(path.c_str()) == 0;
}
struct tm FileHandler::time_to_tm(time_t t)
{
#ifdef WIN32
#if _MSC_VER < 1400 //VS2003
return *localtime(&t);
#else //vs2005->vs2013->
struct tm tt = { 0 };
localtime_s(&tt, &t);
return tt;
#endif
#else //linux
struct tm tt = { 0 };
localtime_r(&t, &tt);
return tt;
#endif
}
bool FileHandler::rollback(const std::string& path, int depth, int max_depth)
{
if (!is_file(path))
{
return true;
}
if (depth > max_depth)
{
return remove_file(path);
}
std::string next_path = path;
size_t pos = path.find_last_not_of("0123456789");
if (pos != std::string::npos && path.at(pos) == '.')
{
next_path = path.substr(0, pos - 0);
}
next_path += ".";
next_path += std::to_string(depth);
rollback(next_path, depth + 1, max_depth);
int ret = ::rename(path.c_str(), next_path.c_str());
(void)ret;
return true;
}
inline int short_path(const char* path, int len)
{
int count = 3;
if (path == nullptr || len <= 0)
{
return 0;
}
const char* last = path + len;
while (last-- != path)
{
if (*last == '/' || *last == '\\')
{
if (--count <= 0)
{
return (int)(last - path + 1);
}
}
}
return 0;
}
}
class UDPHandler
{
public:
#ifndef WIN32
using SOCKET = int;
static const int INVALID_SOCKET = -1;
#endif
public:
UDPHandler()
{
handler_ = INVALID_SOCKET;
}
~UDPHandler()
{
if (handler_ != INVALID_SOCKET)
{
close();
}
}
bool is_open()
{
return handler_ != INVALID_SOCKET;
}
void open()
{
handler_ = socket(AF_INET, SOCK_DGRAM, 0);
}
void close()
{
if (handler_ != INVALID_SOCKET)
{
#ifndef WIN32
::close(handler_);
#else
closesocket(handler_);
#endif
handler_ = INVALID_SOCKET;
}
}
void write(unsigned int ip, unsigned short port, const char* data, int len)
{
if (handler_ == INVALID_SOCKET)
{
return;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = ip;
int ret = sendto(handler_, data, len, 0, (struct sockaddr*) &addr, sizeof(addr));
(void)ret;
}
public:
char chunk_1_[128];
SOCKET handler_;
};
#endif
/*
*
* MIT License
*
* Copyright (C) 2019 YaweiZhang <yawei.zhang@foxmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ===============================================================================
*
* (end of COPYRIGHT)
*/
/*
* AUTHORS: YaweiZhang <yawei.zhang@foxmail.com>
* VERSION: 1.0.0
* PURPOSE: fn-log is a cpp-based logging utility.
* CREATION: 2019.4.20
* RELEASED: 2019.6.27
* QQGROUP: 524700770
*/
#pragma once
#ifndef _FN_LOG_DATA_H_
#define _FN_LOG_DATA_H_
#ifndef FN_LOG_MAX_CHANNEL_SIZE
#define FN_LOG_MAX_CHANNEL_SIZE 2
#endif
#ifndef FN_LOG_MAX_LOG_SIZE
#define FN_LOG_MAX_LOG_SIZE 1000
#endif
#ifndef FN_LOG_MAX_LOG_QUEUE_SIZE //the size need big than push log thread count
#define FN_LOG_MAX_LOG_QUEUE_SIZE 10000
#endif
#ifndef FN_LOG_HOTUPDATE_INTERVEL
#define FN_LOG_HOTUPDATE_INTERVEL 5
#endif
#ifndef FN_LOG_USE_SHM
#define FN_LOG_USE_SHM 0
#endif
#ifndef FN_LOG_SHM_KEY
#define FN_LOG_SHM_KEY 0x9110
#endif
namespace FNLog
{
enum LogPriority
{
PRIORITY_TRACE = 0,
PRIORITY_DEBUG,
PRIORITY_INFO,
PRIORITY_WARN,
PRIORITY_ERROR,
PRIORITY_ALARM,
PRIORITY_FATAL,
PRIORITY_MAX
};
enum LogPrefix
{
LOG_PREFIX_NULL = 0x0,
LOG_PREFIX_TIMESTAMP = 0x1,
LOG_PREFIX_PRIORITY = 0x2,
LOG_PREFIX_THREAD = 0x4,
LOG_PREFIX_FILE = 0x8,
LOG_PREFIX_FUNCTION = 0x10,
LOG_PREFIX_ALL = 0xff
};
enum LogType
{
LOG_TYPE_NULL,
LOG_TYPE_MAX,
};
enum LogState
{
MARK_INVALID,
MARK_HOLD,
MARK_READY
};
struct LogData
{
public:
static const int LOG_SIZE = FN_LOG_MAX_LOG_SIZE;
public:
std::atomic_int data_mark_; //0 invalid, 1 hold, 2 ready
int channel_id_;
int priority_;
int category_;
long long timestamp_; //create timestamp
int precise_; //create time millionsecond suffix
unsigned int thread_;
int content_len_;
char content_[LOG_SIZE]; //content
};
enum DeviceOutType
{
DEVICE_OUT_NULL,
DEVICE_OUT_SCREEN,
DEVICE_OUT_FILE,
DEVICE_OUT_UDP,
};
enum DeviceConfigEnum
{
DEVICE_CFG_ABLE,
DEVICE_CFG_PRIORITY,
DEVICE_CFG_CATEGORY,
DEVICE_CFG_CATEGORY_EXTEND,
DEVICE_CFG_FILE_LIMIT_SIZE,
DEVICE_CFG_FILE_ROLLBACK,
DEVICE_CFG_UDP_IP,
DEVICE_CFG_UDP_PORT,
DEVICE_CFG_MAX_ID
};
enum DeviceLogEnum
{
DEVICE_LOG_CUR_FILE_SIZE,
DEVICE_LOG_CUR_FILE_CREATE_TIMESTAMP,
DEVICE_LOG_CUR_FILE_CREATE_DAY,
DEVICE_LOG_LAST_TRY_CREATE_TIMESTAMP,
DEVICE_LOG_LAST_TRY_CREATE_ERROR,
DEVICE_LOG_TOTAL_WRITE_LINE,
DEVICE_LOG_TOTAL_WRITE_BYTE,
DEVICE_LOG_MAX_ID
};
struct Device
{
public:
static const int MAX_PATH_SYS_LEN = 255;
static const int MAX_PATH_LEN = 200;
static const int MAX_NAME_LEN = 50;
static const int MAX_ROLLBACK_LEN = 4;
static const int MAX_ROLLBACK_PATHS = 5;
static_assert(MAX_PATH_LEN + MAX_NAME_LEN + MAX_ROLLBACK_LEN < MAX_PATH_SYS_LEN, "");
static_assert(LogData::LOG_SIZE > MAX_PATH_SYS_LEN*2, "unsafe size"); // promise format length: date, time, source file path, function length.
static_assert(MAX_ROLLBACK_PATHS < 10, "");
using ConfigFields = std::array<std::atomic_llong, DEVICE_CFG_MAX_ID>;
using LogFields = std::array<std::atomic_llong, DEVICE_LOG_MAX_ID>;
public:
int device_id_;
unsigned int out_type_;
char out_file_[MAX_NAME_LEN];
char out_path_[MAX_PATH_LEN];
ConfigFields config_fields_;
LogFields log_fields_;
};
enum ChannelType
{
CHANNEL_ASYNC,
CHANNEL_SYNC,
};
enum ChannelConfigEnum
{
CHANNEL_CFG_PRIORITY,
CHANNEL_CFG_CATEGORY,
CHANNEL_CFG_CATEGORY_EXTEND,
CHANNEL_CFG_MAX_ID
};
enum ChannelLogEnum
{
CHANNEL_LOG_HOLD,
CHANNEL_LOG_PUSH,
CHANNEL_LOG_PROCESSED = CHANNEL_LOG_PUSH + 8,
CHANNEL_LOG_MAX_ID
};
enum ChannelState
{
CHANNEL_STATE_NULL = 0,
CHANNEL_STATE_RUNNING,
CHANNEL_STATE_WAITING_FINISH,
CHANNEL_STATE_FINISH,
};
struct RingBuffer
{
public:
static const int BUFFER_LEN = FN_LOG_MAX_LOG_QUEUE_SIZE;
public:
char chunk_1_[CHUNK_SIZE];
std::atomic_int write_idx_;
char chunk_2_[CHUNK_SIZE];
std::atomic_int hold_idx_;
char chunk_3_[CHUNK_SIZE];
std::atomic_int read_idx_;
char chunk_4_[CHUNK_SIZE];
std::atomic_int proc_idx_;
char chunk_5_[CHUNK_SIZE];
LogData buffer_[BUFFER_LEN];
};
struct Channel
{
public:
using ConfigFields = std::array<std::atomic_llong, CHANNEL_CFG_MAX_ID>;
using LogFields = std::array<std::atomic_llong, CHANNEL_LOG_MAX_ID>;
static const int MAX_DEVICE_SIZE = 20;
public:
char chunk_1_[CHUNK_SIZE];
int channel_id_;
int channel_type_;
unsigned int channel_state_;
time_t yaml_mtime_;
time_t last_hot_check_;
int chunk_;
int device_size_;
Device devices_[MAX_DEVICE_SIZE];
ConfigFields config_fields_;
LogFields log_fields_;
};
enum LoggerState
{
LOGGER_STATE_UNINIT = 0,
LOGGER_STATE_INITING,
LOGGER_STATE_RUNNING,
LOGGER_STATE_CLOSING,
};
struct SHMLogger
{
static const int MAX_CHANNEL_SIZE = FN_LOG_MAX_CHANNEL_SIZE;
using Channels = std::array<Channel, MAX_CHANNEL_SIZE>;
using RingBuffers = std::array<RingBuffer, MAX_CHANNEL_SIZE>;
int shm_id_;
int shm_size_;
int channel_size_;
Channels channels_;
RingBuffers ring_buffers_;
};
template<class Mutex>
class AutoGuard
{
public:
using mutex_type = Mutex;
inline explicit AutoGuard(Mutex& mtx, bool noop = false) : mutex_(mtx), noop_(noop)
{
if (!noop_)
{
mutex_.lock();
}
}
inline ~AutoGuard() noexcept
{
if (!noop_)
{
mutex_.unlock();
}
}
AutoGuard(const AutoGuard&) = delete;
AutoGuard& operator=(const AutoGuard&) = delete;
private:
Mutex& mutex_;
bool noop_;
};
class Logger
{
public:
static const int MAX_CHANNEL_SIZE = SHMLogger::MAX_CHANNEL_SIZE;
static const int HOTUPDATE_INTERVEL = FN_LOG_HOTUPDATE_INTERVEL;
using ReadLocks = std::array<std::mutex, MAX_CHANNEL_SIZE>;
using ReadGuard = AutoGuard<std::mutex>;
using AsyncThreads = std::array<std::thread, MAX_CHANNEL_SIZE>;
using FileHandles = std::array<FileHandler, MAX_CHANNEL_SIZE* Channel::MAX_DEVICE_SIZE>;
using UDPHandles = std::array<UDPHandler, MAX_CHANNEL_SIZE* Channel::MAX_DEVICE_SIZE>;
public:
using StateLock = std::recursive_mutex;
using StateLockGuard = AutoGuard<StateLock>;
using ScreenLock = std::mutex;
using ScreenLockGuard = AutoGuard<ScreenLock>;
public:
Logger();
~Logger();
bool hot_update_;
std::string yaml_path_;
unsigned int logger_state_;
StateLock state_lock;
SHMLogger* shm_;
ReadLocks read_locks_;
AsyncThreads async_threads;
ScreenLock screen_lock_;
FileHandles file_handles_;
UDPHandles udp_handles_;
};
template<class V>
inline V FN_MIN(V x, V y)
{
return y < x ? y : x;
}
template<class V>
inline V FN_MAX(V x, V y)
{
return x < y ? y : x;
}
template <class M>
inline long long AtomicLoadC(M& m, unsigned eid)
{
return m.config_fields_[eid].load(std::memory_order_relaxed);
}
template <class M>
inline long long AtomicLoadL(M& m, unsigned eid)
{
return m.log_fields_[eid].load(std::memory_order_relaxed);
}
template <class M>
inline void AtomicAddL(M& m, unsigned eid)
{
m.log_fields_[eid].fetch_add(1, std::memory_order_relaxed);
}
template <class M>
inline void AtomicAddLV(M& m, unsigned eid, long long v)
{
m.log_fields_[eid].fetch_add(v, std::memory_order_relaxed);
}
template <class M>
inline void AtomicStoreL(M& m, unsigned eid, long long v)
{
m.log_fields_[eid].store(v, std::memory_order_relaxed);
}
}
#endif
/*
*
* MIT License
*
* Copyright (C) 2019 YaweiZhang <yawei.zhang@foxmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ===============================================================================
*
* (end of COPYRIGHT)
*/
/*
* AUTHORS: YaweiZhang <yawei.zhang@foxmail.com>
* VERSION: 1.0.0
* PURPOSE: fn-log is a cpp-based logging utility.
* CREATION: 2019.4.20
* RELEASED: 2019.6.27
* QQGROUP: 524700770
*/
#pragma once
#ifndef _FN_LOG_PARSE_H_
#define _FN_LOG_PARSE_H_
namespace FNLog
{
enum ParseErrorCode
{
PEC_NONE,
PEC_ERROR,
PEC_ILLEGAL_CHARACTER,
PEC_ILLEGAL_KEY,
PEC_NOT_CLOSURE,
PEC_ILLEGAL_ADDR_IP,
PEC_ILLEGAL_ADDR_PORT,
PEC_UNDEFINED_DEVICE_KEY,
PEC_UNDEFINED_DEVICE_TYPE,
PEC_UNDEFINED_CHANNEL_KEY,
PEC_UNDEFINED_GLOBAL_KEY,
PEC_DEVICE_NOT_ARRAY,
PEC_DEVICE_INDEX_OUT_MAX,
PEC_DEVICE_INDEX_NOT_SEQUENCE,
PEC_CHANNEL_NOT_ARRAY,
PEC_CHANNEL_INDEX_OUT_MAX,
PEC_CHANNEL_INDEX_NOT_SEQUENCE,
PEC_NO_ANY_CHANNEL,
};
enum LineType
{
LINE_NULL,
LINE_ARRAY,
LINE_BLANK,
LINE_EOF,
};
enum BlockType
{
BLOCK_BLANK,
BLOCK_PRE_KEY,
BLOCK_KEY,
BLOCK_PRE_SEP,
BLOCK_PRE_VAL,
BLOCK_VAL,
BLOCK_CLEAN,
};
enum ReseveKey
{
RK_NULL,
RK_CHANNEL,
RK_DEVICE,
RK_SYNC,
RK_DISABLE,
RK_HOT_UPDATE,
RK_PRIORITY,
RK_CATEGORY,
RK_CATEGORY_EXTEND,
RK_OUT_TYPE,
RK_FILE,
RK_PATH,
RK_LIMIT_SIZE,
RK_ROLLBACK,
RK_UDP_ADDR,
};
#if __GNUG__ && __GNUC__ >= 5
#pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif
inline ReseveKey ParseReserve(const char* begin, const char* end)
{
if (end - begin < 2)
{
return RK_NULL;
}
switch (*begin)