-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogging.c
More file actions
821 lines (745 loc) · 35.5 KB
/
logging.c
File metadata and controls
821 lines (745 loc) · 35.5 KB
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
/******************************************************************************/
/* Copyright 2021 Keyfactor */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain a */
/* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless */
/* required by applicable law or agreed to in writing, software distributed */
/* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES */
/* OR CONDITIONS OF ANY KIND, either express or implied. See the License for */
/* thespecific language governing permissions and limitations under the */
/* License. */
/******************************************************************************/
#include "logging.h"
#include "agent.h"
#include "config.h"
#include "utils.h"
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOG_HEAD_SIZE 50
#define LOG_LEVEL_SIZE 10
#define MAX_LOG_SIZE 1024 + LOG_HEAD_SIZE + LOG_LEVEL_SIZE
#define ERRORLVL "[ERROR] "
#define WARNLVL "[WARNING]"
#define INFOLVL "[INFO] "
#define VERBOSELVL "[VERBOSE]"
#define DEBUGLVL "[DEBUG] "
#define TRACELVL "[TRACE] "
#ifdef __QATESTING__
#define QALVL "[QA] "
#endif
#define MAX_FILE_SIZE (5ul * 1024ul * 1024ul) /* 5MByte log file on disk */
#define MAX_HEAP_SIZE (256 * 1024) /* 256k of memory */
/******************************************************************************/
/************************ LOCAL GLOBAL STRUCTURES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************** LOCAL GLOBAL VARIABLES ****************************/
/******************************************************************************/
static char *log_head = NULL;
static char *log_tail = NULL;
static bool log_is_dirty = false;
static size_t log_file_index = 0; /* Current write position in log file */
static bool _trace = false;
static bool _debug = false;
static bool _verbose = false;
static bool _info = true; /* default logging level */
static bool _warn = true;
static bool _error = true;
static char logFormat[LOG_HEAD_SIZE + MAX_LOG_SIZE + LOG_LEVEL_SIZE];
static char timeBuf[LOG_HEAD_SIZE];
/******************************************************************************/
/************************ LOCAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/* */
/* locally defined function to format the log level with date & message */
/* @param char *buf = place to store the formatted message */
/* @param const char *msgFormat = message to print */
/* @param const char *logLevel = the log level of the message */
/* @return none */
/* */
static inline void get_log_format(char *buf, const char *msgFormat,
const char *logLevel) {
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
if (!tm) {
(void)snprintf(buf, MAX_LOG_SIZE, "[%s] - %s - %s\n",
"0000-00-00 00:00:00", logLevel, msgFormat);
} else {
(void)strftime(timeBuf, LOG_HEAD_SIZE, "%Y-%m-%d %H:%M:%S", tm);
(void)snprintf(buf, MAX_LOG_SIZE, "[%s] - %s - %s\n", timeBuf, logLevel,
msgFormat);
}
} /* get_log_format */
/**
* @brief Load the LogFileIndex from the .index file
*
* Reads the write position from a separate .index file that persists
* independently of the config file.
*
* @return The LogFileIndex value from the .index file, or 0 if file doesn't
* exist
*/
static size_t load_log_index(void) {
if (!ConfigData->LogFile) {
return 0;
}
char indexPath[PATH_MAX];
snprintf(indexPath, sizeof(indexPath), "%s.index", ConfigData->LogFile);
FILE *fp = fopen(indexPath, "r");
if (fp) {
size_t index = 0;
if (fscanf(fp, "%zu", &index) == 1) {
fclose(fp);
printf("%s::%s(%d) : Loaded LogFileIndex from .index file: %lu\n",
LOG_INF, index);
return index;
}
fclose(fp);
}
return 0;
} /* load_log_index */
/**
* @brief Save the LogFileIndex to the .index file
*
* Persists the current write position to a separate .index file.
* This survives config file resets.
*
* @return None
*/
static void save_log_index(void) {
if (!ConfigData->LogFile) {
return;
}
char indexPath[PATH_MAX];
snprintf(indexPath, sizeof(indexPath), "%s.index", ConfigData->LogFile);
FILE *fp = fopen(indexPath, "w");
if (fp) {
fprintf(fp, "%zu\n", log_file_index);
fclose(fp);
printf("%s::%s(%d) : Saved LogFileIndex to .index file: %lu\n", LOG_INF,
log_file_index);
} else {
printf("%s::%s(%d) : WARNING: Failed to save .index file\n", LOG_INF);
}
} /* save_log_index */
/**
* @brief Validate and correct LogFileIndex based on actual file size
*
* Implements self-healing logic to recover from config resets or file
* truncation. Uses .index file as primary source, config as fallback,
* and validates against actual file size.
*
* @param[in] fp File pointer to the log file (positioned at end)
* @param[in] actualLogSize Actual size of the log file in bytes
* @return None (updates ConfigData->LogFileIndex directly)
*/
static void validate_and_correct_log_index(FILE *fp, size_t actualLogSize) {
size_t indexFromFile = load_log_index();
bool indexCorrected = false;
printf("%s::%s(%d) : Validating LogFileIndex...\n", LOG_INF);
printf("%s::%s(%d) : Actual file size: %lu\n", LOG_INF, actualLogSize);
printf("%s::%s(%d) : Index from .index file: %lu\n", LOG_INF,
indexFromFile);
/* Use .index file if it exists and is valid */
if (indexFromFile > 0) {
if (indexFromFile > MAX_FILE_SIZE) {
printf("%s::%s(%d) : ERROR: .index file has invalid value (%lu > "
"%lu), ignoring\n",
LOG_INF, indexFromFile, MAX_FILE_SIZE);
log_file_index = 0;
} else {
printf("%s::%s(%d) : Using LogFileIndex from .index file: %lu\n",
LOG_INF, indexFromFile);
log_file_index = indexFromFile;
}
} else {
/* No .index file exists - start at 0 */
log_file_index = 0;
}
/* Validate the index against actual file size */
if (log_file_index > MAX_FILE_SIZE) {
printf("%s::%s(%d) : ERROR: LogFileIndex (%lu) exceeds MAX_FILE_SIZE "
"(%lu), resetting to 0\n",
LOG_INF, log_file_index, MAX_FILE_SIZE);
log_file_index = 0;
indexCorrected = true;
}
/* File hasn't wrapped yet (size < MAX_FILE_SIZE) */
else if (actualLogSize < MAX_FILE_SIZE) {
/* Index points beyond actual file - file was truncated */
if (log_file_index > actualLogSize) {
printf("%s::%s(%d) : WARNING: LogFileIndex (%lu) > file size "
"(%lu), file may have been truncated\n",
LOG_INF, log_file_index, actualLogSize);
printf("%s::%s(%d) : Resetting to end of file\n", LOG_INF);
log_file_index = actualLogSize;
indexCorrected = true;
}
/* Index is 0 but file has data - .index was deleted/missing */
else if (log_file_index == 0 && actualLogSize > 0) {
printf("%s::%s(%d) : WARNING: LogFileIndex is 0 but file has %lu "
"bytes\n",
LOG_INF, actualLogSize);
printf("%s::%s(%d) : .index file may be missing. Resuming "
"at end of file\n",
LOG_INF);
log_file_index = actualLogSize;
indexCorrected = true;
}
/* Valid case: 0 <= index <= actualLogSize */
else {
printf("%s::%s(%d) : LogFileIndex is valid (%lu <= %lu)\n", LOG_INF,
log_file_index, actualLogSize);
}
}
/* File is at or above MAX_FILE_SIZE (circular buffer is active) */
else if (actualLogSize >= MAX_FILE_SIZE) {
if (log_file_index > MAX_FILE_SIZE) {
printf("%s::%s(%d) : ERROR: LogFileIndex (%lu) exceeds "
"MAX_FILE_SIZE (%lu), resetting to 0\n",
LOG_INF, log_file_index, MAX_FILE_SIZE);
log_file_index = 0;
indexCorrected = true;
} else if (log_file_index == 0 && indexFromFile == 0) {
printf("%s::%s(%d) : WARNING: Log file is at maximum size (%lu "
"bytes) and LogFileIndex is 0\n",
LOG_INF, actualLogSize);
printf("%s::%s(%d) : Circular buffer is active. .index "
"file may be missing.\n",
LOG_INF);
printf("%s::%s(%d) : Some old logs may be overwritten.\n",
LOG_INF);
/* Keep LogFileIndex = 0, but warn user */
} else {
printf("%s::%s(%d) : Circular buffer active. Using LogFileIndex "
"%lu in %lu byte file\n",
LOG_INF, log_file_index, actualLogSize);
}
}
/* Save corrected value to .index file */
if (indexCorrected) {
printf("%s::%s(%d) : LogFileIndex corrected to %lu\n", LOG_INF,
log_file_index);
save_log_index();
}
} /* validate_and_correct_log_index */
/* */
/* local-only function to print a message */
/* @returns none */
/* */
static void log_me(const char *fmt, ...) {
get_log_format(logFormat, fmt, "[LOGGING]");
va_list args;
va_start(args, fmt);
(void)vprintf(logFormat, args);
va_end(args);
} /* log_me */
/* */
/* Write the heap data to disk & set the heap tail = heap start */
/* */
/* @param : none */
/* @return : none */
/* */
static void write_heap_to_disk(void) {
do {
if (!ConfigData->LogFile) {
printf("%s::%s(%d) : No Log file defined in config\n", LOG_INF);
break;
}
FILE *fp = NULL;
if (file_exists(ConfigData->LogFile)) {
printf("%s::%s(%d) : Opening existing log file\n", LOG_INF);
fp = fopen(ConfigData->LogFile, "r+");
} else {
printf("%s::%s(%d) : Creating new log file\n", LOG_INF);
fp = fopen(ConfigData->LogFile, "w");
}
if (NULL != fp) {
fseek(fp, 0ul, SEEK_END);
size_t actualLogSize = ftell(fp);
printf("%s::%s(%d) : Opened log file with size %lu\n", LOG_INF,
actualLogSize);
/* Validate and correct LogFileIndex if needed */
validate_and_correct_log_index(fp, actualLogSize);
fseek(fp, log_file_index, SEEK_SET);
size_t writeLen = log_tail - log_head;
size_t logFileTest = (log_file_index + writeLen);
printf("%s::%s(%d) : writing %lu bytes to log at index of %lu\n",
LOG_INF, writeLen, log_file_index);
printf("%s::%s(%d) : MAX_FILE_SIZE = %lu\n", LOG_INF,
MAX_FILE_SIZE);
if (MAX_FILE_SIZE > logFileTest) {
printf("%s::%s(%d) : Writing %lu bytes to disk\n", LOG_INF,
writeLen);
size_t chars_written =
fwrite((void *)log_head, sizeof(*log_head), writeLen, fp);
log_file_index += chars_written;
log_tail = log_head;
} else {
printf("%s::%s(%d) : Log file write of %lu creates wrap of log "
"file\n",
LOG_INF, writeLen);
size_t toEOF = MAX_FILE_SIZE - log_file_index;
size_t chars_written =
fwrite((void *)log_head, sizeof(*log_head), toEOF, fp);
fseek(fp, 0, SEEK_SET); /* reset to beginning of file */
size_t new_chars_written =
fwrite((void *)(log_head + chars_written + 1),
sizeof(*log_head), (writeLen - chars_written), fp);
log_file_index = new_chars_written;
log_tail = log_head;
}
/* Save to .index file only */
save_log_index();
fclose(fp);
} else {
printf("******* Error opening log file %s\n **************",
ConfigData->LogFile);
break;
}
} while (false);
return;
} /* write_heap_to_disk */
/******************************************************************************/
/************************ GLOBAL FUNCTION DEFINITIONS *************************/
/******************************************************************************/
/* */
/* @fn is_log_verbose */
/* @brief check the current state of the verbose logging level */
/* @param none */
/* @returns true if verbose level is enabled, false otherwise */
/* */
bool is_log_verbose(void) { return _verbose; } /* is_log_verbose */
/* */
/* @fn is_log_trace */
/* @brief check the current state of the trace logging level */
/* @param none */
/* @returns true if trace level is enabled, false otherwise */
/* */
bool is_log_trace(void) { return _trace; } /* is_log_trace */
/* */
/* @fn is_log_debug */
/* @brief check the current state of the debug logging level */
/* @param none */
/* @returns true if debug level is enabled, false otherwise */
/* */
bool is_log_debug(void) { return _debug; } /* is_log_debug */
/* */
/* @fn is_log_info */
/* @brief check the current state of the info logging level */
/* @param none */
/* @returns true if info level is enabled, false otherwise */
/* */
bool is_log_info(void) { return _info; } /* is_log_info */
/* */
/* @fn is_log_warn */
/* @brief check the current state of the warning logging level */
/* @param none */
/* @returns true if the warning level is enabled, false otherwise */
/* */
bool is_log_warn(void) { return _warn; } /* is_log_warn */
/* */
/* @fn is_log_error */
/* @brief check the current state of the error logging level */
/* @param none */
/* @returns true if error level is enabled, false otherwise */
/* */
bool is_log_error(void) { return _error; } /* is_log_error */
/* */
/* @fn is_log_off */
/* @brief check if logging is off */
/* @param none */
/* @returns true if the error level is off, false otherwise */
/* */
bool is_log_off(void) { return !_error; } /* is_log_off */
/* */
/* @fn log_error */
/* @brief Print a message if the error logging level is enabled */
/* */
/* NOTE: This function unconditionally sets the global 'success' flag to */
/* false as a side effect, regardless of whether logging is enabled. */
/* Code that calls log_error() through an intermediate function */
/* (e.g., AgentApiResult_log) may snapshot and conditionally restore */
/* 'success' if the logged condition is recoverable. Search for */
/* 'success_before' to locate all such snapshot/restore sites before */
/* modifying this behavior. */
/* */
/* @returns none */
/* */
void log_error(const char *fmt, ...) {
/* NOTE: On error set global success to return EXIT_FAILURE */
success = false;
if (_error) {
get_log_format(logFormat, fmt, ERRORLVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, ERRORLVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_error */
/* */
/* @fn log_warn */
/* @brief Print a message if the info logging level is enabled */
/* @returns none */
/* */
void log_warn(const char *fmt, ...) {
if (_warn) {
get_log_format(logFormat, fmt, WARNLVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, WARNLVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_warn */
/* */
/* @fn log_info */
/* @brief Print a message if the info logging level is enabled */
/* @returns none */
/* */
void log_info(const char *fmt, ...) {
if (_info) {
get_log_format(logFormat, fmt, INFOLVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, INFOLVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_info */
/* */
/* @fn log_verbose */
/* @brief Print a message if the verbose logging level is enabled */
/* @returns none */
/* */
void log_verbose(const char *fmt, ...) {
if (_verbose) {
get_log_format(logFormat, fmt, VERBOSELVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, VERBOSELVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_verbose */
/* */
/* @fn log_debug */
/* @brief Print a message if the debug logging level is enabled */
/* @returns none */
/* */
void log_debug(const char *fmt, ...) {
if (_debug) {
get_log_format(logFormat, fmt, DEBUGLVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, DEBUGLVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_debug */
/* */
/* @fn log_trace */
/* @brief Print a message if the trace logging level is enabled */
/* @returns none */
/* */
void log_trace(const char *fmt, ...) {
if (_trace) {
get_log_format(logFormat, fmt, TRACELVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, TRACELVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
}
} /* log_trace */
#ifdef __QATESTING__
/* */
/* @fn log_qa */
/* @brief Print a message if the info logging level is enabled */
/* @returns none */
/* */
void log_qa(const char *fmt, ...) {
get_log_format(logFormat, fmt, QALVL);
va_list args;
va_start(args, fmt);
size_t chars_to_write = vfprintf(stderr, logFormat, args);
va_end(args);
if (config_loaded) {
/* Write to the log buffer, too */
size_t log_index = (log_tail - log_head); /* parasoft-suppress
* MISRAC2012-DIR_4_1-i
* "same array" */
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
write_heap_to_disk();
}
get_log_format(logFormat, fmt, QALVL);
va_list args_inner;
va_start(args_inner, fmt);
size_t remaining = MAX_HEAP_SIZE - (log_tail - log_head);
size_t chars_written =
vsnprintf(log_tail, remaining, logFormat, args_inner);
va_end(args_inner);
log_tail += chars_written;
log_is_dirty = true;
/* End write to the log buffer, too */
}
} /* log_warn */
#endif
/* */
/* @fn log_set_trace */
/* @brief Turn on the trace & all lower logging levels */
/* @returns none */
/* */
void log_set_trace(bool param) {
log_me("%s::%s(%d) : Setting logging level to trace.", LOG_INF);
_trace = param;
_debug = param;
_verbose = param;
_info = param;
_warn = param;
_error = param;
} /* log_set_trace */
/* */
/* @fn log_set_debug */
/* @brief Turn on the debug & all lower logging levels */
/* @returns none */
/* */
void log_set_debug(bool param) {
log_me("%s::%s(%d) : Setting logging level to debug.", LOG_INF);
_trace = !param;
_debug = param;
_verbose = param;
_info = param;
_warn = param;
_error = param;
} /* log_set_debug */
/* */
/* @fn log_set_verbosity */
/* @brief Turn on the verbose & all lower logging levels */
/* @returns none */
/* */
void log_set_verbosity(bool param) {
log_me("%s::%s(%d) : Setting logging level to verbose.", LOG_INF);
_trace = !param;
_debug = !param;
_verbose = param;
_info = param;
_warn = param;
_error = param;
} /* log_set_verbosity */
/* */
/* @fn log_set_info */
/* @brief Turn on the info & all lower logging levels */
/* @returns none */
/* */
void log_set_info(bool param) {
log_me("%s::%s(%d) : Setting logging level to info.", LOG_INF);
_trace = !param;
_debug = !param;
_verbose = !param;
_info = param;
_warn = param;
_error = param;
} /* log_set_info */
/* */
/* @fn log_set_warn */
/* @breif Turn on the warning logging level & all lower logging levels */
/* @returns none */
/* */
void log_set_warn(bool param) {
log_me("%s::%s(%d) : Setting logging level to warning.", LOG_INF);
_trace = !param;
_debug = !param;
_verbose = !param;
_info = !param;
_warn = param;
_error = param;
} /* log_set_warn */
/* */
/* @fn log_set_error */
/* @brief Turn on the error logging level */
/* @returns none */
/* */
void log_set_error(bool param) {
log_me("%s::%s(%d) : Setting logging level to error.", LOG_INF);
_trace = !param;
_debug = !param;
_verbose = !param;
_info = !param;
_warn = !param;
_error = param;
} /* log_set_error */
/* */
/* @fn log_set_off */
/* @brief Turn off all further logging */
/* @returns none */
/* */
void log_set_off(bool param) {
log_me("%s::%s(%d) : Turning off all logging.", LOG_INF);
_trace = !param;
_debug = !param;
_verbose = !param;
_info = !param;
_warn = !param;
_error = !param;
} /* log_set_off */
/* */
/* @fn load_log_buffer */
/* @breif Load the log buffer from the log file */
/* @return none */
/* */
bool load_log_buffer(void) {
bool bResult = false;
log_me("%s::%s(%d) : Creating log buffer", LOG_INF);
log_head = (char *)calloc(MAX_HEAP_SIZE, sizeof(*log_head));
if (log_head) {
log_me("%s::%s(%d) : Successfully created buffer of size %lu", LOG_INF,
MAX_HEAP_SIZE);
log_tail = log_head;
bResult = true;
} else {
log_me("%s::%s(%d) : Out of memory", LOG_INF);
}
return bResult;
} /* load_log_buffer */
/* */
/* @fn write_log_file */
/* @breif Write the log to disk if the buffer is dirty */
/* @return none */
/* */
void write_log_file(void) {
if (log_is_dirty) {
write_heap_to_disk();
} else {
printf("%s::%s(%d) : LOG is not DIRTY\n", LOG_INF);
}
return;
} /* write_log_file */
/* */
/* Free the heap data structure */
/* @param : none */
/* @return : none */
/* */
void free_log_heap(void) {
printf("%s::%s(%d) : Freeing Logging Heap Memory\n", LOG_INF);
if (log_head)
free(log_head);
log_head = NULL;
log_tail = NULL;
return;
} /* free_log_heap */
/******************************************************************************/
/******************************* END OF FILE **********************************/
/******************************************************************************/