Browse Source
changed class tool. Now multiple interface per class are supported as well as simple inheritence.
master
changed class tool. Now multiple interface per class are supported as well as simple inheritence.
master
27 changed files with 519 additions and 430 deletions
-
98include/cclass.h
-
81include/class.h
-
39include/interface.h
-
30include/interface/class.h
-
22include/interface/logger.h
-
49include/logger.h
-
14include/server.h
-
18include/socket.h
-
119src/cclass.c
-
28src/class.c
-
6src/daemonize.c
-
45src/interface.c
-
43src/interface/class.c
-
37src/interface/logger.c
-
35src/logger.c
-
11src/logger/add.c
-
30src/logger/log.c
-
16src/logger/stderr.c
-
29src/logger/syslog.c
-
27src/server.c
-
3src/server/close_conn.c
-
34src/server/run.c
-
39src/socket.c
-
24src/socket/accept.c
-
15src/socket/connect.c
-
17src/socket/listen.c
-
10src/testserver.c
@ -1,98 +0,0 @@ |
|||
/** |
|||
* \file |
|||
* cclass.h: basic "class-like" handling of code and data structures |
|||
* Copyright (C) 2011 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef __CCLASS_H__ |
|||
#define __CCLASS_H__ |
|||
|
|||
#include <stdarg.h> |
|||
#include <sys/types.h> |
|||
#include <json/json.h> |
|||
|
|||
#define CCLASS_MAGIC 0xFEFE |
|||
|
|||
|
|||
typedef void (* ctor)(void *, va_list *); |
|||
typedef void (* clr)(void *); |
|||
typedef void (* dtor)(void *); |
|||
typedef void (* jCtor)(void *, struct json_object *); |
|||
typedef void (* jTo)(void *, struct json_object **); |
|||
|
|||
typedef struct _CCLASS { |
|||
const int magic; |
|||
size_t size; |
|||
ctor __construct; |
|||
clr __clear; |
|||
dtor __destruct; |
|||
jCtor __jsonConst; |
|||
jTo __toJson; |
|||
} * CCLASS; |
|||
|
|||
#define CCLASS_SIZE sizeof(struct _CCLASS) |
|||
#define CCLASS_PTR_SIZE sizeof(CCLASS) |
|||
|
|||
#define __construct(_class) \ |
|||
static void __construct(_class this, va_list * params) |
|||
#define __clear(_class) \ |
|||
static void __clear(_class this) |
|||
#define __destruct(_class) \ |
|||
static void __destruct(_class this) |
|||
#define __jsonConst(_class) \ |
|||
static void __jsonConst(_class this, struct json_object * json) |
|||
#define __toJson(_class) \ |
|||
static void __toJson(_class this, struct json_object ** json) |
|||
|
|||
#define CLASS(_class) \ |
|||
struct _##_class; \ |
|||
typedef struct _##_class * _class; \ |
|||
extern const CCLASS const __##_class; \ |
|||
struct _##_class |
|||
|
|||
#define INIT_CLASS(_class) \ |
|||
__construct(_class); \ |
|||
__clear(_class); \ |
|||
__destruct(_class); \ |
|||
__jsonConst(_class); \ |
|||
__toJson(_class); \ |
|||
static const struct _CCLASS _cclass = { \ |
|||
CCLASS_MAGIC, \ |
|||
sizeof(struct _##_class), \ |
|||
(ctor)__construct, \ |
|||
(clr)__clear, \ |
|||
(dtor)__destruct, \ |
|||
(jCtor)__jsonConst, \ |
|||
(jTo)__toJson \ |
|||
}; const CCLASS __##_class = (const CCLASS)&_cclass |
|||
|
|||
void * _new(const CCLASS _class, ...); |
|||
#define new(_class, ...) _new((__##_class), __VA_ARGS__) |
|||
|
|||
void * _newFromJson(const CCLASS _class, struct json_object * json); |
|||
#define newFromJson(_class, json) _newFromJson((__##_class), (json)) |
|||
|
|||
int _instanceOf(const CCLASS _class, void * _object); |
|||
#define instanceOf(_class, object) _instanceOf((__##_class), (object)) |
|||
|
|||
void clear(void * _object); |
|||
void delete(void * _object); |
|||
void toJson(void * _object, struct json_object ** json); |
|||
int isObject(void * _object); |
|||
|
|||
#endif//__CCLASS_H__ |
|||
|
|||
// vim: set et ts=4 sw=4: |
|||
@ -0,0 +1,81 @@ |
|||
#ifndef __CLASS_H__ |
|||
#define __CLASS_H__ |
|||
|
|||
#include <stdarg.h> |
|||
#include <sys/types.h> |
|||
#include <string.h> |
|||
#include <assert.h> |
|||
|
|||
#include "interface.h" |
|||
|
|||
#define _ISOC99_SOURCE |
|||
|
|||
#define CLASS_MAGIC 0xFEFE |
|||
|
|||
#define CLASS(name) \ |
|||
struct c_##name; \ |
|||
typedef struct c_##name * name; \ |
|||
extern struct class * const _##name; \ |
|||
struct c_##name |
|||
|
|||
#define EXTENDS(parent) \ |
|||
const char _[sizeof(struct c_##parent)] |
|||
|
|||
#define _NULL NULL |
|||
#define CREATE_CLASS(name,_parent,...) \ |
|||
static struct class c_##name; \ |
|||
static void _classInit_(void) { \ |
|||
c_##name.parent = _##_parent; \ |
|||
c_##name.init = NULL; \ |
|||
} \ |
|||
static struct class c_##name = { \ |
|||
CLASS_MAGIC, \ |
|||
NULL, \ |
|||
sizeof(struct c_##name), \ |
|||
_classInit_, \ |
|||
INIT_IMPL(__VA_ARGS__) \ |
|||
}; struct class * const _##name = &c_##name |
|||
|
|||
/** |
|||
* @TODO: actually i use gcc feature ## for variadoc... think about |
|||
* a way to make this standard. |
|||
*/ |
|||
#define CALL(object,_iface,method,...) \ |
|||
do { \ |
|||
class_ptr class = class_getClass((object)); \ |
|||
struct i_##_iface * iface; \ |
|||
if (class->init) class->init(); \ |
|||
iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \ |
|||
while ((NULL == iface || NULL == iface->method) && HAS_PARENT(class)) { \ |
|||
class = class->parent; \ |
|||
if (class->init) class->init(); \ |
|||
iface = (struct i_##_iface *)class_getInterface(&class, &i_##_iface); \ |
|||
}; \ |
|||
assert(NULL != iface->method); \ |
|||
iface->method(object, ##__VA_ARGS__); \ |
|||
} while(0) |
|||
|
|||
|
|||
#define IFACE_GET(class,iface) (interfaceGet(&((class)->impl),(iface))) |
|||
#define IFACE_EXISTS(class,iface) (NULL != IFACE_GET((class),(iface))) |
|||
|
|||
#define HAS_PARENT(class) (NULL != ((class)->parent)) |
|||
|
|||
typedef void (* fptr_classInit)(void); |
|||
|
|||
struct class; |
|||
typedef struct class * class_ptr; |
|||
struct class { |
|||
const int magic; |
|||
class_ptr parent; |
|||
size_t object_size; |
|||
fptr_classInit init; |
|||
struct iface_impl impl; |
|||
}; |
|||
|
|||
extern inline void * class_getInterface(class_ptr *, iface_ptr); |
|||
extern inline class_ptr class_getClass(void *); |
|||
|
|||
#endif // __CLASS_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,39 @@ |
|||
#ifndef __INTERFACE_H__ |
|||
#define __INTERFACE_H__ |
|||
|
|||
#include <sys/types.h> |
|||
|
|||
#define MAX_IFACE 32 // ATTENTION: every iface_impl will use MAX_IFACE * sizeof(void*) |
|||
|
|||
#define IFACE(name) ((const struct i_##name const*)&i_##name##_impl) |
|||
|
|||
#define INIT_IFACE(name,...) \ |
|||
static const struct i_##name i_##name##_impl = {&i_##name,__VA_ARGS__} |
|||
|
|||
#define NUMARGS(...) (sizeof((const void*[]){__VA_ARGS__})/sizeof(void*)) |
|||
|
|||
#define INIT_IMPL(...) {NUMARGS(__VA_ARGS__), 0, {__VA_ARGS__}} |
|||
#define CREATE_IMPL(...) \ |
|||
static struct iface_impl iface_impl = INIT_IMPL(__VA_ARGS__) |
|||
|
|||
#define METHOD_GET(iface,method) (iface->method) |
|||
|
|||
|
|||
struct interface { |
|||
const char * name; |
|||
const size_t nmethods; |
|||
}; |
|||
typedef const struct interface * iface_ptr; |
|||
|
|||
struct iface_impl { |
|||
const size_t nimpl; // number of interface implementations |
|||
char simpl; // implementations sorted?? |
|||
const void * impl[MAX_IFACE]; // implementations |
|||
}; |
|||
typedef struct iface_impl * iface_impl_ptr; |
|||
|
|||
extern struct interface * interfaceGet(iface_impl_ptr, const iface_ptr); |
|||
|
|||
#endif // __INTERFACE_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,30 @@ |
|||
#ifndef __INTERFACE_CLASS_H__ |
|||
#define __INTERFACE_CLASS_H__ |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface.h" |
|||
|
|||
typedef void (* fptr_ctor)(void *, va_list *); |
|||
typedef void (* fptr_dtor)(void *); |
|||
typedef void (* fptr_clone)(void *, const void * const); |
|||
|
|||
extern const struct interface i_Class; |
|||
|
|||
struct i_Class { |
|||
const struct interface * const _; |
|||
fptr_ctor ctor; |
|||
fptr_dtor dtor; |
|||
fptr_clone clone; |
|||
}; |
|||
|
|||
extern inline void * classNew(class_ptr, ...); |
|||
extern inline void classDelete(void **); |
|||
|
|||
#define new(class,...) classNew(_##class, __VA_ARGS__) |
|||
#define delete(object) classDelete((void **)(object)) |
|||
|
|||
#endif // __INTERFACE_CLASS_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,22 @@ |
|||
#ifndef __INTERFACE_LOGGER_H__ |
|||
#define __INTERFACE_LOGGER_H__ |
|||
|
|||
#include <stdarg.h> |
|||
|
|||
#include "interface.h" |
|||
#include "logger.h" |
|||
|
|||
typedef void (* fptr_log)(void *, logger_level, const char * const); |
|||
|
|||
extern const struct interface i_Logger; |
|||
|
|||
struct i_Logger { |
|||
const struct interface * const _; |
|||
fptr_log log; |
|||
}; |
|||
|
|||
extern inline void loggerLog(void *, logger_level, const char * const, ...); |
|||
|
|||
#endif // __INTERFACE_LOGGER_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,34 +1,33 @@ |
|||
#ifndef __LOGGER_H__ |
|||
#define __LOGGER_H__ |
|||
|
|||
#include "cclass.h" |
|||
|
|||
|
|||
#define LOGGER_EMERG 0 |
|||
#define LOGGER_ALERT 1 |
|||
#define LOGGER_CRIT 2 |
|||
#define LOGGER_ERR 3 |
|||
#define LOGGER_WARNING 4 |
|||
#define LOGGER_NOTICE 5 |
|||
#define LOGGER_INFO 6 |
|||
#define LOGGER_DEBUG 7 |
|||
|
|||
#define MAX_LOG_FNCTS 10 |
|||
|
|||
|
|||
typedef void (*logger_logfnct)(int level, const char * msg); |
|||
|
|||
|
|||
CLASS(LOGGER) { |
|||
logger_logfnct logfncts[MAX_LOG_FNCTS]; |
|||
unsigned int logfncts_count; |
|||
#include "class.h" |
|||
|
|||
typedef enum logger_level { |
|||
LOGGER_DEBUG=0, |
|||
LOGGER_INFO, |
|||
LOGGER_NOTICE, |
|||
LOGGER_WARNING, |
|||
LOGGER_ERR, |
|||
LOGGER_CRIT, |
|||
LOGGER_ALERT, |
|||
LOGGER_EMERG |
|||
} logger_level; |
|||
|
|||
extern const char * const logger_level_str[]; |
|||
|
|||
CLASS(Logger) { |
|||
logger_level min_level; |
|||
}; |
|||
|
|||
void logger_log(LOGGER this, int level, const char * msg, ...) |
|||
__attribute__((format (printf, 3, 4))); |
|||
CLASS(LoggerStderr) { |
|||
EXTENDS(Logger); |
|||
}; |
|||
|
|||
void logger_add(LOGGER this, logger_logfnct logfunc); |
|||
CLASS(LoggerSyslog) { |
|||
EXTENDS(Logger); |
|||
}; |
|||
|
|||
#endif /* __LOGGER_H__ */ |
|||
#endif // __LOGGER_H__ |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,119 +0,0 @@ |
|||
/** |
|||
* \file |
|||
* cclass.c: basic "class-like" handling of code and data structures |
|||
* Copyright (C) 2011 Georg Hopp |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU General Public License as published by |
|||
* the Free Software Foundation, either version 3 of the License, or |
|||
* (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#include <stdarg.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <json/json.h> |
|||
|
|||
#include "cclass.h" |
|||
|
|||
#undef __construct |
|||
#undef __clear |
|||
#undef __destruct |
|||
#undef __jsonConst |
|||
#undef __toJson |
|||
|
|||
void * |
|||
_new(const CCLASS _class, ...) |
|||
{ |
|||
const CCLASS class = _class; |
|||
void * object = calloc(1, class->size + sizeof(void *)); |
|||
|
|||
* (const struct _CCLASS **)object = class; |
|||
object += sizeof(CCLASS); |
|||
|
|||
if (class->__construct) { |
|||
va_list params; |
|||
|
|||
va_start(params, _class); |
|||
class->__construct(object, ¶ms); |
|||
va_end(params); |
|||
} |
|||
|
|||
return object; |
|||
} |
|||
|
|||
void * |
|||
_newFromJson(const CCLASS _class, struct json_object * json) |
|||
{ |
|||
const CCLASS class = _class; |
|||
void * object = calloc(1, class->size + sizeof(CCLASS)); |
|||
|
|||
* (const struct _CCLASS **) object = class; |
|||
object += sizeof(CCLASS); |
|||
|
|||
if (class->__jsonConst && json) { |
|||
class->__jsonConst(object, json); |
|||
} |
|||
|
|||
return object; |
|||
} |
|||
|
|||
int |
|||
_instanceOf(const CCLASS _class, void * _object) |
|||
{ |
|||
const CCLASS * class = _object - sizeof(CCLASS); |
|||
|
|||
return (class && _class == *class); |
|||
} |
|||
|
|||
|
|||
void |
|||
clear(void * _object) |
|||
{ |
|||
const CCLASS * class = _object - sizeof(CCLASS); |
|||
|
|||
if (_object && *class && (*class)->__clear) { |
|||
(*class)->__clear(_object); |
|||
} |
|||
} |
|||
|
|||
void |
|||
delete(void ** _object) |
|||
{ |
|||
const CCLASS * class = (*_object) - sizeof(CCLASS); |
|||
|
|||
if (*_object && *class && (*class)->__destruct) { |
|||
(*class)->__destruct(*_object); |
|||
} |
|||
|
|||
free((void *)class); |
|||
*_object = NULL; |
|||
} |
|||
|
|||
void |
|||
toJson(void * _object, struct json_object ** json) |
|||
{ |
|||
const CCLASS * class = _object - sizeof(CCLASS); |
|||
|
|||
if (_object && *class && (*class)->__toJson) { |
|||
(*class)->__toJson(_object, json); |
|||
} |
|||
} |
|||
|
|||
int |
|||
isObject(void * _object) |
|||
{ |
|||
const CCLASS * class = _object - sizeof(CCLASS); |
|||
|
|||
return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); |
|||
} |
|||
|
|||
// vim: set et ts=4 sw=4: |
|||
@ -0,0 +1,28 @@ |
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface.h" |
|||
|
|||
inline |
|||
void * |
|||
class_getInterface(class_ptr * class, iface_ptr _iface) |
|||
{ |
|||
void * iface = (void *)IFACE_GET(*class, _iface); |
|||
|
|||
while(NULL == iface && HAS_PARENT(*class)) { |
|||
*class = (*class)->parent; |
|||
iface = (void *)IFACE_GET(*class, _iface); |
|||
} |
|||
|
|||
return iface; |
|||
} |
|||
|
|||
inline |
|||
class_ptr |
|||
class_getClass(void * object) |
|||
{ |
|||
return *(class_ptr *)(object - sizeof(void*)); |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,45 @@ |
|||
#include <sys/types.h> |
|||
#include <stdlib.h> |
|||
|
|||
#include "interface.h" |
|||
|
|||
#ifndef TRUE |
|||
#define TRUE 1 |
|||
#endif // TRUE |
|||
|
|||
static |
|||
inline |
|||
int |
|||
comp(const void * _a, const void * _b) |
|||
{ |
|||
const struct interface * a = **(const struct interface ***)_a; |
|||
const struct interface * b = **(const struct interface ***)_b; |
|||
return ((a)<(b))? -1 : ((a)>(b))? 1 : 0; |
|||
} |
|||
|
|||
/** |
|||
* this one is important in selector functions to get the correct interface |
|||
* implementation of a class. |
|||
*/ |
|||
struct interface * |
|||
interfaceGet(iface_impl_ptr iface_impl, const iface_ptr _iface) |
|||
{ |
|||
const iface_ptr * iface = &_iface; |
|||
void * dummy; |
|||
|
|||
if (! iface_impl->simpl) { |
|||
qsort((void**)(iface_impl->impl), iface_impl->nimpl, sizeof(iface_ptr), comp); |
|||
iface_impl->simpl=TRUE; |
|||
} |
|||
|
|||
dummy = bsearch( |
|||
&iface, |
|||
iface_impl->impl, |
|||
iface_impl->nimpl, |
|||
sizeof(iface_ptr), |
|||
comp); |
|||
|
|||
return dummy? *(struct interface **)dummy : dummy; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,43 @@ |
|||
#include <stdarg.h> |
|||
#include <stdlib.h> |
|||
#include <assert.h> |
|||
|
|||
#include "class.h" |
|||
#include "interface/class.h" |
|||
|
|||
const |
|||
struct interface i_Class = { |
|||
"class", |
|||
3 |
|||
}; |
|||
|
|||
inline |
|||
void * |
|||
classNew(class_ptr class, ...) |
|||
{ |
|||
void * object = calloc(1, class->object_size + sizeof(void*)); |
|||
va_list params; |
|||
|
|||
if (class->init) class->init(); |
|||
|
|||
* (class_ptr *)object = class; |
|||
object += sizeof(void*); |
|||
|
|||
va_start(params, class); |
|||
CALL(object, Class, ctor, ¶ms); |
|||
va_end(params); |
|||
|
|||
return object; |
|||
} |
|||
|
|||
inline |
|||
void |
|||
classDelete(void ** object) |
|||
{ |
|||
CALL(*object, Class, dtor); |
|||
|
|||
free(*object - sizeof(void*)); |
|||
*object = NULL; |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,37 @@ |
|||
#include <stdlib.h> |
|||
|
|||
#include "logger.h" |
|||
#include "interface/logger.h" |
|||
|
|||
const struct interface i_Logger = { |
|||
"logger", |
|||
1 |
|||
}; |
|||
|
|||
inline |
|||
void |
|||
loggerLog(void * _object, logger_level level, const char * const fmt, ...) { |
|||
Logger object = _object; |
|||
|
|||
if (level >= object->min_level) { |
|||
char * msg = NULL; |
|||
size_t msg_size = 0; |
|||
va_list params; |
|||
|
|||
va_start(params, fmt); |
|||
msg_size = vsnprintf(msg, msg_size, fmt, params); |
|||
va_end(params); |
|||
|
|||
msg = malloc(msg_size + 1); |
|||
|
|||
va_start(params, fmt); |
|||
vsnprintf(msg, msg_size + 1, fmt, params); |
|||
va_end(params); |
|||
|
|||
CALL(_object, Logger, log, level, msg); |
|||
|
|||
free(msg); |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,18 +1,33 @@ |
|||
#include "logger.h" |
|||
#include <stdarg.h> |
|||
|
|||
extern void logger_syslog(int level, const char * msg); |
|||
#include "logger.h" |
|||
#include "interface/class.h" |
|||
#include "interface/logger.h" |
|||
|
|||
INIT_CLASS(LOGGER); |
|||
const |
|||
char * const |
|||
logger_level_str[] = { |
|||
"DEBUG", |
|||
"INFO", |
|||
"NOTICE", |
|||
"WARNING", |
|||
"ERR", |
|||
"CRIT", |
|||
"ALERT", |
|||
"EMERG" |
|||
}; |
|||
|
|||
__construct(LOGGER) |
|||
static |
|||
void |
|||
ctor(void * _this, va_list * params) |
|||
{ |
|||
this->logfncts[0] = logger_syslog; |
|||
this->logfncts_count = 1; |
|||
Logger this = _this; |
|||
this->min_level = va_arg(*params, int); |
|||
} |
|||
|
|||
__destruct(LOGGER) {} |
|||
__jsonConst(LOGGER) {} |
|||
__toJson(LOGGER) {} |
|||
__clear(LOGGER) {} |
|||
static void dtor(void * _this) {} |
|||
|
|||
INIT_IFACE(Class, ctor, dtor, NULL); |
|||
CREATE_CLASS(Logger, NULL, IFACE(Class)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,11 +0,0 @@ |
|||
#include "logger.h" |
|||
|
|||
void |
|||
logger_add(LOGGER this, logger_logfnct logfunc) { |
|||
if (this->logfncts_count < MAX_LOG_FNCTS) { |
|||
this->logfncts[this->logfncts_count] = logfunc; |
|||
this->logfncts_count++; |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,30 +0,0 @@ |
|||
#define _ISOC99_SOURCE |
|||
|
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include "logger.h" |
|||
|
|||
void |
|||
logger_log(LOGGER this, int level, const char * message, ...) { |
|||
va_list args; |
|||
char buffer[1025]; |
|||
logger_logfnct * logfnct; |
|||
|
|||
int maxBuf = sizeof(buffer)/sizeof(buffer[0]); |
|||
|
|||
memset(buffer, 0, maxBuf); |
|||
|
|||
va_start(args, message); |
|||
vsnprintf(buffer, 1024, message, args); |
|||
va_end(args); |
|||
|
|||
logfnct = this->logfncts; |
|||
|
|||
while (NULL != *logfnct) { |
|||
(*logfnct)(level, buffer); |
|||
logfnct++; |
|||
} |
|||
} |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -0,0 +1,16 @@ |
|||
#include <stdio.h> |
|||
|
|||
#include "logger.h" |
|||
#include "interface/logger.h" |
|||
|
|||
static |
|||
void |
|||
logStderr(void * this, logger_level level, const char * const msg) |
|||
{ |
|||
fprintf(stderr, "[%s] %s\n", logger_level_str[level], msg); |
|||
} |
|||
|
|||
INIT_IFACE(Logger, logStderr); |
|||
CREATE_CLASS(LoggerStderr, Logger, IFACE(Logger)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
@ -1,20 +1,29 @@ |
|||
#include <syslog.h> |
|||
|
|||
const int priority[] = { |
|||
LOG_USER | LOG_EMERG, |
|||
LOG_USER | LOG_ALERT, |
|||
LOG_USER | LOG_CRIT, |
|||
LOG_USER | LOG_ERR, |
|||
LOG_USER | LOG_WARNING, |
|||
LOG_USER | LOG_NOTICE, |
|||
#include "logger.h" |
|||
#include "interface/logger.h" |
|||
|
|||
static |
|||
const |
|||
int syslog_priority[] = { |
|||
LOG_USER | LOG_DEBUG, |
|||
LOG_USER | LOG_INFO, |
|||
LOG_USER | LOG_DEBUG |
|||
LOG_USER | LOG_NOTICE, |
|||
LOG_USER | LOG_WARNING, |
|||
LOG_USER | LOG_ERR, |
|||
LOG_USER | LOG_CRIT, |
|||
LOG_USER | LOG_ALERT, |
|||
LOG_USER | LOG_EMERG |
|||
}; |
|||
|
|||
static |
|||
void |
|||
logger_syslog(int level, const char * msg) |
|||
logSyslog(void * this, logger_level level, const char * const msg) |
|||
{ |
|||
syslog(priority[level], "%s", msg); |
|||
syslog(syslog_priority[level], "[%s] %s", logger_level_str[level], msg); |
|||
} |
|||
|
|||
INIT_IFACE(Logger, logSyslog); |
|||
CREATE_CLASS(LoggerSyslog, Logger, IFACE(Logger)); |
|||
|
|||
// vim: set ts=4 sw=4: |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue