aheinecke@404: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik aheinecke@404: * Software engineering by Intevation GmbH aheinecke@404: * aheinecke@404: * This file is Free Software under the GNU GPL (v>=2) aheinecke@404: * and comes with ABSOLUTELY NO WARRANTY! aheinecke@404: * See LICENSE.txt for details. aheinecke@404: */ aheinecke@252: #ifndef COMMON_LOGGING_H aheinecke@252: #define COMMON_LOGGING_H aheinecke@252: aheinecke@252: #ifdef __cplusplus aheinecke@252: extern "C" { aheinecke@252: #endif aheinecke@252: aheinecke@252: /* aheinecke@252: * @file aheinecke@252: * @brief Logging and debugging functions aheinecke@252: */ aheinecke@252: aheinecke@503: #include andre@623: #include aheinecke@503: andre@615: /** @def Maximum length of log messages */ andre@615: #define MAX_LOG 511 andre@615: andre@615: /** @def The name used for logging */ andre@615: #define LOG_NAME "TrustBridge" andre@615: andre@1058: extern bool g_debug; andre@1058: aheinecke@252: #ifdef WIN32 aheinecke@252: aheinecke@252: #include aheinecke@252: aheinecke@252: /** @brief Gets the localized error message for the last error aheinecke@252: * returned by GetLastError aheinecke@252: * aheinecke@252: * @returns utf8 error message that needs to be freed by the caller. aheinecke@252: **/ aheinecke@252: aheinecke@252: char *getLastErrorMsg(); aheinecke@252: aheinecke@252: #endif aheinecke@252: aheinecke@252: /** aheinecke@252: * @def DEBUGOUTPUT aheinecke@252: * @brief If defined code for extra debugging output will be generated. aheinecke@252: */ aheinecke@252: #define DEBUGOUTPUT aheinecke@252: aheinecke@252: /** aheinecke@252: * @def DEBUGPREFIX aheinecke@252: * @brief A string prepended to debug output. aheinecke@252: * aheinecke@252: * Should be defined to indicate which module created the output. aheinecke@252: */ aheinecke@252: #ifndef DEBUGPREFIX aheinecke@252: #define DEBUGPREFIX "" aheinecke@252: #endif aheinecke@252: aheinecke@252: /** aheinecke@252: * @def DEBUGPRINTF(fmt, ...) aheinecke@252: * @brief Debug printf aheinecke@252: * aheinecke@252: * Prints to stderr if DEBUGOUTPUT is defined. aheinecke@252: */ aheinecke@252: #ifdef DEBUGOUTPUT aheinecke@329: # ifndef WIN32 andre@1060: # define DEBUGPRINTF(fmt, ...) if (g_debug) fprintf(stderr, DEBUGPREFIX "DEBUG: " fmt, ##__VA_ARGS__); aheinecke@329: # else /* WIN32 */ aheinecke@329: # define DEBUGPRINTF(fmt, ...) \ andre@1060: if (g_debug) \ aheinecke@329: { \ aheinecke@329: char buf[512]; \ aheinecke@329: snprintf(buf, 511, "DEBUG: " fmt, ##__VA_ARGS__); \ aheinecke@329: buf[511] = '\0'; \ aheinecke@329: OutputDebugStringA(buf); \ aheinecke@329: } aheinecke@329: # endif /* WIN32 */ aheinecke@287: #else aheinecke@287: # define DEBUGPRINTF(fmt, ...) aheinecke@287: #endif aheinecke@287: aheinecke@287: /** aheinecke@287: * @def ERRORPRINTF(fmt, ...) aheinecke@287: * @brief Debug printf aheinecke@287: * aheinecke@287: * Prints an error to stderr aheinecke@287: */ aheinecke@504: #ifdef WIN32 aheinecke@504: # define ERRORPRINTF(fmt, ...) \ aheinecke@504: { \ aheinecke@504: char buf[512]; \ aheinecke@504: snprintf(buf, 511, "ERROR: " fmt, ##__VA_ARGS__); \ aheinecke@504: buf[511] = '\0'; \ aheinecke@504: OutputDebugStringA(buf); \ aheinecke@504: } aheinecke@504: #else aheinecke@504: # define ERRORPRINTF(fmt, ...) fprintf(stderr, DEBUGPREFIX "ERROR: " fmt, ##__VA_ARGS__); aheinecke@504: #endif aheinecke@287: aheinecke@513: /** aheinecke@513: * @def PRINTLASTERROR(msg) aheinecke@513: * @brief Prints the last windows error with a custom message aheinecke@513: * aheinecke@513: * Prints an error to stderr aheinecke@513: */ aheinecke@503: #define PRINTLASTERROR(msg) \ aheinecke@503: char *my_error = getLastErrorMsg(); \ aheinecke@503: if (my_error) { \ aheinecke@503: ERRORPRINTF(msg" : %s\n", my_error); \ aheinecke@503: free (my_error); \ andre@1060: } else \ andre@1060: ERRORPRINTF ("Failed to get error information\n"); aheinecke@287: aheinecke@287: andre@615: /** andre@615: * @brief log an informational message into the syslog / event log andre@615: * andre@615: * The message length is limited to MAX_LOG characters. Log messages andre@615: * are expected to be in UTF-8 encoding. andre@615: * andre@615: * Function paramters are the same as for the printf familiy. andre@615: */ andre@615: void syslog_info_printf(const char *format, ...); andre@615: andre@615: /** andre@615: * @brief log an error message into the syslog / event log andre@615: * andre@615: * The message length is limited to MAX_LOG characters. Log messages andre@615: * are expected to be in UTF-8 encoding. andre@615: * andre@615: * Function paramters are the same as for the printf familiy. andre@615: */ andre@615: void syslog_error_printf(const char *format, ...); andre@615: andre@623: /** andre@625: * @brief log a certificate install / remove event from base64 data. andre@623: * andre@623: * Logs a message in the event / syslog to mark a certificate andre@623: * installation or removal. andre@623: * andre@623: * @param[in] store name of the certificate store. andre@623: * @param[in] b64cert base64 encoded certificate. andre@623: * @param[in] install weather to log this as installation or removal andre@623: */ andre@623: void log_certificate(const char *store, char *b64cert, bool install); andre@904: andre@625: /** andre@625: * @brief log a certificate install / remove event from der data. andre@625: * andre@625: * Logs a message in the event / syslog to mark a certificate andre@625: * installation or removal. andre@625: * andre@625: * @param[in] store name of the certificate store. andre@625: * @param[in] der_data pointer to der data of the certificate. andre@625: * @param[in] der_size size of the der_data andre@625: * @param[in] install weather to log this as installation or removal andre@625: */ andre@625: void log_certificate_der(const char *store, unsigned char *der_data, size_t der_size, bool install); andre@623: aheinecke@252: #ifdef __cplusplus aheinecke@252: } aheinecke@252: #endif aheinecke@252: aheinecke@252: #endif /* COMMON_LOGGING_H */