NingSun | 0c89b3c | 2018-02-08 08:34:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 SURFnet bv |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /***************************************************************************** |
| 28 | log.cpp |
| 29 | |
| 30 | Implements logging functions. This file is based on the concepts from |
| 31 | SoftHSM v1 but extends the logging functions with support for a variable |
| 32 | argument list as defined in stdarg (3). |
| 33 | *****************************************************************************/ |
| 34 | |
| 35 | #include "config.h" |
| 36 | #include <stdarg.h> |
| 37 | #include <syslog.h> |
| 38 | #include <stdio.h> |
| 39 | #include <sstream> |
| 40 | #include <vector> |
| 41 | #include "log.h" |
| 42 | |
| 43 | int softLogLevel = LOG_DEBUG; |
| 44 | |
| 45 | bool setLogLevel(const std::string &loglevel) |
| 46 | { |
| 47 | if (loglevel == "ERROR") |
| 48 | { |
| 49 | softLogLevel = LOG_ERR; |
| 50 | } |
| 51 | else if (loglevel == "WARNING") |
| 52 | { |
| 53 | softLogLevel = LOG_WARNING; |
| 54 | } |
| 55 | else if (loglevel == "INFO") |
| 56 | { |
| 57 | softLogLevel = LOG_INFO; |
| 58 | } |
| 59 | else if (loglevel == "DEBUG") |
| 60 | { |
| 61 | softLogLevel = LOG_DEBUG; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | ERROR_MSG("Unknown value (%s) for log.level in configuration", loglevel.c_str()); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void softHSMLog(const int loglevel, const char* functionName, const char* fileName, const int lineNo, const char* format, ...) |
| 73 | { |
| 74 | if (loglevel > softLogLevel) return; |
| 75 | |
| 76 | std::stringstream prepend; |
| 77 | |
| 78 | #ifdef SOFTHSM_LOG_FILE_AND_LINE |
| 79 | prepend << fileName << "(" << lineNo << ")"; |
| 80 | #ifndef SOFTHSM_LOG_FUNCTION_NAME |
| 81 | (void) functionName; |
| 82 | prepend << ":"; |
| 83 | #endif // !SOFTHSM_LOG_FUNCTION_NAME |
| 84 | prepend << " "; |
| 85 | #endif // SOFTHSM_LOG_FILE_AND_LINE |
| 86 | |
| 87 | #ifdef SOFTHSM_LOG_FUNCTION_NAME |
| 88 | prepend << functionName << ": "; |
| 89 | #endif // SOFTHSM_LOG_FUNCTION_NAME |
| 90 | |
| 91 | // Print the format to a log message |
| 92 | std::vector<char> logMessage; |
| 93 | va_list args; |
| 94 | |
| 95 | logMessage.resize(4096); |
| 96 | |
| 97 | va_start(args, format); |
| 98 | vsnprintf(&logMessage[0], 4096, format, args); |
| 99 | va_end(args); |
| 100 | |
| 101 | // And log it |
| 102 | syslog(loglevel, "%s%s", prepend.str().c_str(), &logMessage[0]); |
| 103 | |
| 104 | #ifdef DEBUG_LOG_STDERR |
| 105 | fprintf(stderr, "%s%s\n", prepend.str().c_str(), &logMessage[0]); |
| 106 | fflush(stderr); |
| 107 | #endif // DEBUG_LOG_STDERR |
| 108 | } |
| 109 | |