nfv/guest-client/guest-client-3.0.1/guest_client_api/guest_api_debug.c

139 lines
5.0 KiB
C
Executable File

/*
* Copyright (c) 2013-2016, Wind River Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3) Neither the name of Wind River Systems nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "guest_api_debug.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "guest_api_types.h"
#define GUEST_DEBUG_WANT_SYSLOG
#ifdef GUEST_DEBUG_WANT_SYSLOG
#include <syslog.h>
#endif
static char _process_name[30];
static GuestApiDebugLogLevelT _log_level = GUEST_API_DEBUG_LOG_LEVEL_INFO;
// ****************************************************************************
// Guest API Debug - Log Level String
// ==================================
const char* guest_api_debug_log_level_str( GuestApiDebugLogLevelT level )
{
switch (level) {
case GUEST_API_DEBUG_LOG_LEVEL_ERROR: return "error";
case GUEST_API_DEBUG_LOG_LEVEL_INFO: return " info";
case GUEST_API_DEBUG_LOG_LEVEL_DEBUG: return "debug";
case GUEST_API_DEBUG_LOG_LEVEL_VERBOSE: return " verb";
default:
return "???";
}
}
// ****************************************************************************
// ****************************************************************************
// Guest API Debug - Set Log Level
// ===============================
void guest_api_debug_set_log_level( GuestApiDebugLogLevelT level )
{
_log_level = level;
}
// ****************************************************************************
// ****************************************************************************
// Guest API Debug - Want Log
// ==========================
bool guest_api_debug_want_log( GuestApiDebugLogLevelT level )
{
return (level <= _log_level);
}
// ****************************************************************************
// ****************************************************************************
// Guest API Debug - Log
// =====================
void guest_api_debug_log( const char* format, ... )
{
char time_str[80];
char date_str[32];
struct tm t_real;
struct timespec ts_real;
va_list arguments;
char log_data[256];
va_start(arguments, format);
vsnprintf(log_data, sizeof(log_data), format, arguments);
va_end(arguments);
clock_gettime(CLOCK_REALTIME, &ts_real);
if (NULL == localtime_r(&(ts_real.tv_sec), &t_real))
{
snprintf( time_str, sizeof(time_str),
"YYYY:MM:DD HH:MM:SS.xxx" );
} else {
strftime( date_str, sizeof(date_str), "%b %e %H:%M:%S",
&t_real );
snprintf( time_str, sizeof(time_str), "%s.%03ld", date_str,
ts_real.tv_nsec/1000000 );
}
#ifdef GUEST_DEBUG_WANT_SYSLOG
syslog(LOG_INFO, "%s", log_data);
#else
printf("%s %s: %s\n", time_str, _process_name, log_data);
#endif
}
// ****************************************************************************
// ****************************************************************************
// Guest API Debug - Initialize
// ============================
GuestApiErrorT guest_api_debug_initialize( char process_name[] )
{
_log_level = GUEST_API_DEBUG_LOG_LEVEL_INFO;
snprintf(_process_name, sizeof(_process_name), "%s", process_name);
return GUEST_API_OKAY;
}
// ****************************************************************************
// ****************************************************************************
// Guest API Debug - Finalize
// ==========================
GuestApiErrorT guest_api_debug_finalize( void )
{
_log_level = GUEST_API_DEBUG_LOG_LEVEL_INFO;
_process_name[0] = '\0';
return GUEST_API_OKAY;
}
// ****************************************************************************