Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
log.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
63# ifndef LOG_H
64# define LOG_H
65
66# include <sys/utsname.h>
67# include <errno.h>
68# include <stdio.h>
69# include <stdarg.h>
70# include <string.h>
71# include <useMutex.H>
72
92{
93 static const short int Max_Log_Line_Length = 512;
94 static const short int Max_Time_Line_Length = 61;
95
97
99
102
103 size_t num_lines;
105
111 void get_time()
112 {
114 time(&unix_time);
115
116 struct tm broken_time;
118
119 strftime(time_buffer, Max_Time_Line_Length, "%a %b %d %G %r",
120 &broken_time);
121 }
122
123public:
124
137
140 {
142 }
143
153# ifdef DEBUG
154 char * file,
155 int line,
156# endif
157 const char *format, ...)
158 {
159 va_list ap;
160
162 {
164
165 get_time();
166
167# ifdef DEBUG
168 fprintf(file_descriptor, "%s (%s:%d:%ld:%d) : ",
169 time_buffer, file, line, pthread_self(), getpid());
170# else
171 fprintf(file_descriptor, "%s (%ld:%d): ",
173# endif
175 }
176 va_end(ap);
177 }
178
188# ifdef DEBUG
189 char * file,
190 int line,
191# endif
192 const char *format, ...)
193 {
194 va_list ap;
195
197 {
199
200 get_time();
201
202# ifdef DEBUG
203 fprintf(file_descriptor, "%s (%s:%d:%ld:%d) : ",
204 time_buffer, file, line, pthread_self(), getpid());
205# else
206 fprintf(file_descriptor, "%s (%ld:%d): ",
208# endif
211 }
212 va_end(ap);
213 }
214
215
216};
217
224# ifdef DEBUG
225# define WRITE_IN_LOG(log, format...) log.write_line(__FILE__, __LINE__, format)
226# else
227# define WRITE_IN_LOG(log, format...) log.write_line(format)
228# endif
229
230# endif // LOG_H
Thread-safe log manager with timestamps.
Definition log.H:92
size_t num_lines
Lines written (not used)
Definition log.H:103
char print_buffer[Max_Log_Line_Length]
Message buffer.
Definition log.H:100
void get_time()
Get current time as formatted string.
Definition log.H:111
~Log_Manager()
Destructor releases mutex.
Definition log.H:139
static const short int Max_Time_Line_Length
Max timestamp length.
Definition log.H:94
void init_line(const char *format,...)
Write log line without newline (for multi-part messages).
Definition log.H:152
void write_line(const char *format,...)
Write complete log line with newline.
Definition log.H:187
static const short int Max_Log_Line_Length
Max message length.
Definition log.H:93
pthread_mutex_t mutex
Thread synchronization mutex.
Definition log.H:98
char time_buffer[Max_Time_Line_Length]
Timestamp buffer.
Definition log.H:101
size_t max_num_lines
Max lines (not used)
Definition log.H:104
FILE * file_descriptor
Output file descriptor.
Definition log.H:96
Log_Manager(FILE *_file_descriptor)
Construct log manager for a file.
Definition log.H:130
DynList< T > maps(const C &c, Op op)
Classic map operation.
fstream file[12]
Definition treapObs.C:67
void init_mutex(pthread_mutex_t *m)
Initialize a pthread mutex (pointer version)
Definition useMutex.C:41
void destroy_mutex(pthread_mutex_t *m)
Destroy a pthread mutex (pointer version)
Definition useMutex.C:54
RAII-style wrapper for POSIX mutexes.
#define CRITICAL_SECTION(mutex)
Create an anonymous critical section guard.
Definition useMutex.H:233