Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions | Variables
Log.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Config.h"
#include <unistd.h>
#include "Log.h"

Go to the source code of this file.

Macros

#define _Class   _Log
 

Functions

static void _log (const Log *self, LogLevel level, const char *fmt, va_list args)
 
Class_Log (void)
 
static void dealloc (Object *self)
 
static void debug (const Log *self, const char *fmt,...)
 
static void destroy (Class *clazz)
 
static void error (const Log *self, const char *fmt,...)
 
static void fatal (const Log *self, const char *fmt,...)
 
static void flush (const Log *self)
 
static void info (const Log *self, const char *fmt,...)
 
static Loginit (Log *self)
 
static void initialize (Class *clazz)
 
static LoginitWithName (Log *self, const char *name)
 
static LogsharedInstance (void)
 
static void trace (const Log *self, const char *fmt,...)
 
static void warn (const Log *self, const char *fmt,...)
 

Variables

static Log_sharedInstance
 

Macro Definition Documentation

◆ _Class

#define _Class   _Log

Definition at line 39 of file Log.c.

Function Documentation

◆ _log()

static void _log ( const Log self,
LogLevel  level,
const char *  fmt,
va_list  args 
)
static

Definition at line 158 of file Log.c.

158 {
159
160 const char *levels[] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" };
161 assert(level < lengthof(levels));
162
163 if (level < self->level) {
164 return;
165 }
166
167 assert(self->file);
168
169 const time_t date = time(NULL);
170 const struct tm *localDate = localtime(&date);
171
172 char buffer[128];
173 strftime(buffer, sizeof(buffer), self->format, localDate);
174
175 char *c = buffer;
176 while (*c) {
177
178 if (*c == '%') {
179 _Bool token = true;
180
181 if (*(c + 1) == 'n') {
182 fputs(self->name, self->file);
183 } else if (*(c + 1) == 'l') {
184 fputs(levels[level], self->file);
185 } else if (*(c + 1) == 'm') {
186 vfprintf(self->file, fmt, args);
187 } else {
188 token = false;
189 }
190
191 if (token) {
192 c++;
193 c++;
194 continue;
195 }
196 }
197
198 fputc(*c, self->file);
199 c++;
200 }
201
202 fputc('\n', self->file);
203 fflush(self->file);
204}
#define lengthof(array)
Definition: Types.h:145
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
const char * format
The format string, defaults to LOG_FORMAT_DEFAULT. This string is post-processed after date substitut...
Definition: Log.h:82
FILE * file
The file descriptor (defaults to stdout).
Definition: Log.h:88
char * name
The name of this Log.
Definition: Log.h:98

◆ _Log()

Class * _Log ( void  )

Definition at line 285 of file Log.c.

285 {
286 static Class *clazz;
287 static Once once;
288
289 do_once(&once, {
290 clazz = _initialize(&(const ClassDef) {
291 .name = "Log",
292 .superclass = _Object(),
293 .instanceSize = sizeof(Log),
294 .interfaceOffset = offsetof(Log, interface),
295 .interfaceSize = sizeof(LogInterface),
297 .destroy = destroy,
298 });
299 });
300
301 return clazz;
302}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void destroy(Class *clazz)
Definition: Log.c:276
static void initialize(Class *clazz)
Definition: Log.c:256
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
A Log4J-inspired log appender.
Definition: Log.h:61
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 46 of file Log.c.

46 {
47
48 Log *this = (Log *) self;
49
50 if (!isatty(fileno(this->file))) {
51 const int err = fclose(this->file);
52 assert(err == 0);
53 }
54
55 free(this->name);
56
57 super(Object, self, dealloc);
58}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ debug()

static void debug ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 66 of file Log.c.

66 {
67
68 va_list args;
69 va_start(args, fmt);
70
71 $(self, log, LogLevelDebug, fmt, args);
72
73 va_end(args);
74}
@ LogLevelDebug
Definition: Log.h:42
void log(const Log *self, LogLevel level, const char *fmt, va_list args)
Write a message to the Log.

◆ destroy()

static void destroy ( Class clazz)
static
See also
Class::destroy(Class *)

Definition at line 276 of file Log.c.

276 {
277
279}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static Log * _sharedInstance
Definition: Log.c:206

◆ error()

static void error ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 80 of file Log.c.

80 {
81
82 va_list args;
83 va_start(args, fmt);
84
85 $(self, log, LogLevelError, fmt, args);
86
87 va_end(args);
88}
@ LogLevelError
Definition: Log.h:45

◆ fatal()

static void fatal ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 94 of file Log.c.

94 {
95
96 va_list args;
97 va_start(args, fmt);
98
99 $(self, log, LogLevelFatal, fmt, args);
100
101 va_end(args);
102}
@ LogLevelFatal
Definition: Log.h:46

◆ flush()

static void flush ( const Log self)
static

Definition at line 108 of file Log.c.

108 {
109
110 assert(self->file);
111 fflush(self->file);
112}

◆ info()

static void info ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 118 of file Log.c.

118 {
119
120 va_list args;
121 va_start(args, fmt);
122
123 $(self, log, LogLevelInfo, fmt, args);
124
125 va_end(args);
126}
@ LogLevelInfo
Definition: Log.h:43

◆ init()

static Log * init ( Log self)
static

Definition at line 132 of file Log.c.

132 {
133
134 return $(self, initWithName, NULL);
135}
Log * initWithName(Log *self, const char *name)
Initializes this Log with the specified name.
Definition: Log.c:141

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 256 of file Log.c.

256 {
257
258 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
259
260 ((LogInterface *) clazz->interface)->debug = debug;
261 ((LogInterface *) clazz->interface)->error = error;
262 ((LogInterface *) clazz->interface)->fatal = fatal;
263 ((LogInterface *) clazz->interface)->flush = flush;
264 ((LogInterface *) clazz->interface)->info = info;
265 ((LogInterface *) clazz->interface)->init = init;
266 ((LogInterface *) clazz->interface)->initWithName = initWithName;
267 ((LogInterface *) clazz->interface)->log = _log;
268 ((LogInterface *) clazz->interface)->trace = trace;
269 ((LogInterface *) clazz->interface)->sharedInstance = sharedInstance;
270 ((LogInterface *) clazz->interface)->warn = warn;
271}
static void _log(const Log *self, LogLevel level, const char *fmt, va_list args)
Definition: Log.c:158
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
void warn(const Log *self, const char *fmt,...)
Log a warn message.
Definition: Log.c:241
void info(const Log *self, const char *fmt,...)
Log an info message.
Definition: Log.c:118
Log * sharedInstance(void)
Definition: Log.c:212
void flush(const Log *self)
Flushes and pending output to this Log's file.
Definition: Log.c:108
void trace(const Log *self, const char *fmt,...)
Log a trace message.
Definition: Log.c:227
void fatal(const Log *self, const char *fmt,...)
Log a fatal message.
Definition: Log.c:94
void debug(const Log *self, const char *fmt,...)
Log a debug message.
Definition: Log.c:66
void error(const Log *self, const char *fmt,...)
Log an error message.
Definition: Log.c:80

◆ initWithName()

static Log * initWithName ( Log self,
const char *  name 
)
static

Definition at line 141 of file Log.c.

141 {
142
143 self = (Log *) super(Object, self, init);
144 if (self) {
145 self->name = strdup(name ?: "default");
146 self->level = LogLevelInfo;
148 self->file = stdout;
149 }
150
151 return self;
152}
#define LOG_FORMAT_DEFAULT
The default Log format.
Definition: Log.h:52
LogLevel level
The LogLevel of this Log.
Definition: Log.h:93

◆ sharedInstance()

static Log * sharedInstance ( void  )
static

Definition at line 212 of file Log.c.

212 {
213
214 static Once once;
215
216 do_once(&once, {
218 });
219
220 return _sharedInstance;
221}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159

◆ trace()

static void trace ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 227 of file Log.c.

227 {
228
229 va_list args;
230 va_start(args, fmt);
231
232 $(self, log, LogLevelTrace, fmt, args);
233
234 va_end(args);
235}
@ LogLevelTrace
Definition: Log.h:41

◆ warn()

static void warn ( const Log self,
const char *  fmt,
  ... 
)
static

Definition at line 241 of file Log.c.

241 {
242
243 va_list args;
244 va_start(args, fmt);
245
246 $(self, log, LogLevelWarn, fmt, args);
247
248 va_end(args);
249}
@ LogLevelWarn
Definition: Log.h:44

Variable Documentation

◆ _sharedInstance

Log* _sharedInstance
static

Definition at line 206 of file Log.c.