ESAPI-C 1.0
The OWASP Enterprise Security API for C

log.c

Go to the documentation of this file.
00001 
00008 #include <stdlib.h>
00009 #include <string.h>
00010 
00011 #define DEFAULT_LOG_CATEGORY_NAME "log4c.category.default"
00012 
00013 #include "log.h"
00014 
00015 char *eventTypeNames[] = { "SECURITY_SUCCESS", "SECURITY_FAILURE",
00016                 "EVENT_SUCCESS", "EVENT_FAILURE" };
00017 
00018 int esapi_open_log() {
00019         return log4c_init();
00020 }
00021 
00022 int esapi_close_log() {
00023         return log4c_fini();
00024 }
00025 
00026 void _log(int priority, const user *u, int type, const char *fmt, ...) {
00027         va_list va;
00028         char *message;
00029 
00030         if (fmt) {
00031                 va_start(va, fmt);
00032                 if ((message = (char *) malloc(MAX_MESSAGE_LEN)) != 0) {
00033                         if (vsnprintf(message, MAX_MESSAGE_LEN, fmt, va)) {
00034                                 char *username;
00035                                 if (u && u->name) {
00036                                         username = (char *) u->name;
00037                                 } else {
00038                                         username = "APPLICATION"; // non-attributable event
00039                                 }
00040                                 log4c_category_log(
00041                                                 log4c_category_get(DEFAULT_LOG_CATEGORY_NAME),
00042                                                 LOG4C_PRIORITY_DEBUG, "[%s %s] %s",
00043                                                 eventTypeNames[type], username, message);
00044                         }
00045                 }
00046                 free(message);
00047                 va_end(va);
00048         }
00049 }
00050 
00051 /*
00052  * What about unattributed events?
00053  */
00054 void esapi_log_trace(const user *u, int type, const char *fmt, ...) {
00055         va_list va;
00056         if ((fmt) && (u)) {
00057                 va_start(va, fmt);
00058                 _log(LOG4C_PRIORITY_TRACE, u, type, fmt, va);
00059                 va_end(va);
00060         }
00061 }
00062 
00063 void esapi_log_debug(const user *u, int type, const char *fmt, ...) {
00064         va_list va;
00065         if ((fmt) && (u)) {
00066                 va_start(va, fmt);
00067                 _log(LOG4C_PRIORITY_DEBUG, u, type, fmt, va);
00068                 va_end(va);
00069         }
00070 }
00071 
00072 void esapi_log_info(const user *u, int type, const char *fmt, ...) {
00073         va_list va;
00074         if ((fmt) && (u)) {
00075                 va_start(va, fmt);
00076                 _log(LOG4C_PRIORITY_INFO, u, type, fmt, va);
00077                 va_end(va);
00078         }
00079 }
00080 
00081 void esapi_log_warn(const user *u, int type, const char *fmt, ...) {
00082         va_list va;
00083         if ((fmt) && (u)) {
00084                 va_start(va, fmt);
00085                 _log(LOG4C_PRIORITY_WARN, u, type, fmt, va);
00086                 va_end(va);
00087         }
00088 }
00089 
00090 void esapi_log_error(const user *u, int type, const char *fmt, ...) {
00091         va_list va;
00092         if ((fmt) && (u)) {
00093                 va_start(va, fmt);
00094                 _log(LOG4C_PRIORITY_ERROR, u, type, fmt, va);
00095                 va_end(va);
00096         }
00097 }
00098 
00099 void esapi_log_fatal(const user *u, int type, const char *fmt, ...) {
00100         va_list va;
00101         if ((fmt) && (u)) {
00102                 va_start(va, fmt);
00103                 _log(LOG4C_PRIORITY_FATAL, u, type, fmt, va);
00104                 va_end(va);
00105         }
00106 }
 All Data Structures Files Functions Variables Typedefs Defines