📄 ncbi_util.c
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_util.c,v $ * PRODUCTION Revision 1000.1 2003/11/17 22:18:57 gouriano * PRODUCTION PRODUCTION: UPGRADED [ORIGINAL] Dev-tree R6.31 * PRODUCTION * =========================================================================== *//* $Id: ncbi_util.c,v 1000.1 2003/11/17 22:18:57 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Anton Lavrentiev * * File Description: * Auxiliary (optional) code for "ncbi_core.[ch]" * */#include "ncbi_config.h"#include "ncbi_priv.h"#ifndef NCBI_CXX_TOOLKIT# include <ncbistd.h># include <ncbimisc.h># include <ncbitime.h>#else# include <ctype.h># include <errno.h># include <stdlib.h># include <string.h># include <time.h>#endif/* Static function pre-declarations to avoid C++ compiler warnings */#if defined(__cplusplus)extern "C" { static void s_LOG_FileHandler(void* user_data, SLOG_Handler* call_data); static void s_LOG_FileCleanup(void* user_data);}#endif /* __cplusplus *//****************************************************************************** * MT locking */extern void CORE_SetLOCK(MT_LOCK lk){ if (g_CORE_MT_Lock && lk != g_CORE_MT_Lock) { MT_LOCK_Delete(g_CORE_MT_Lock); } g_CORE_MT_Lock = lk;}extern MT_LOCK CORE_GetLOCK(void){ return g_CORE_MT_Lock;}/****************************************************************************** * ERROR HANDLING and LOGGING */extern void CORE_SetLOG(LOG lg){ CORE_LOCK_WRITE; if (g_CORE_Log && lg != g_CORE_Log) { LOG_Delete(g_CORE_Log); } g_CORE_Log = lg; CORE_UNLOCK;}extern LOG CORE_GetLOG(void){ return g_CORE_Log;}extern void CORE_SetLOGFILE(FILE* fp, int/*bool*/ auto_close){ LOG lg = LOG_Create(0, 0, 0, 0); LOG_ToFILE(lg, fp, auto_close); CORE_SetLOG(lg);}extern int/*bool*/ CORE_SetLOGFILE_NAME(const char* filename){ FILE* fp = fopen(filename, "a"); if ( !fp ) { CORE_LOGF_ERRNO(eLOG_Error, errno, ("Cannot open \"%s\"", filename)); return 0/*false*/; } CORE_SetLOGFILE(fp, 1/*true*/); return 1/*true*/;}static TLOG_FormatFlags s_LogFormatFlags = fLOG_Default;extern TLOG_FormatFlags CORE_SetLOGFormatFlags(TLOG_FormatFlags flags){ TLOG_FormatFlags old_flags = s_LogFormatFlags; s_LogFormatFlags = flags; return old_flags;}extern char* LOG_ComposeMessage(const SLOG_Handler* call_data, TLOG_FormatFlags format_flags){ static const char s_RawData_Begin[] = "\n#################### [BEGIN] Raw Data (%lu byte%s):\n"; static const char s_RawData_End[] = "\n#################### [END] Raw Data\n"; char* str, datetime[32]; /* Calculated length of ... */ size_t datetime_len = 0; size_t level_len = 0; size_t file_line_len = 0; size_t module_len = 0; size_t message_len = 0; size_t data_len = 0; size_t total_len; /* Adjust formatting flags */ if (call_data->level == eLOG_Trace) {#if defined(NDEBUG) && !defined(_DEBUG) if (!(format_flags & fLOG_None))#endif /*NDEBUG && !_DEBUG*/ format_flags |= fLOG_Full; } if (format_flags == fLOG_Default) {#if defined(NDEBUG) && !defined(_DEBUG) format_flags = fLOG_Short;#else format_flags = fLOG_Full;#endif /*NDEBUG && !_DEBUG*/ } /* Pre-calculate total message length */ if ((format_flags & fLOG_DateTime) != 0) {#ifdef NCBI_OS_MSWIN /*Should be compiler-dependent but C-Tkit lacks it*/ _strdate(&datetime[datetime_len]); datetime_len += strlen(&datetime[datetime_len]); datetime[datetime_len++] = ' '; _strtime(&datetime[datetime_len]); datetime_len += strlen(&datetime[datetime_len]); datetime[datetime_len++] = ' '; datetime[datetime_len] = '\0';#else /*NCBI_OS_MSWIN*/ static const char timefmt[] = "%D %T "; struct tm* tm;# ifdef NCBI_CXX_TOOLKIT time_t t = time(0);# ifdef HAVE_LOCALTIME_R struct tm temp; localtime_r(&t, &temp); tm = &temp;# else /*HAVE_LOCALTIME_R*/ tm = localtime(&t);# endif/*HAVE_LOCALTIME_R*/# else /*NCBI_CXX_TOOLKIT*/ struct tm temp; Nlm_GetDayTime(&temp); tm = &temp;# endif/*NCBI_CXX_TOOLKIT*/ datetime_len = strftime(datetime, sizeof(datetime), timefmt, tm);#endif/*NCBI_OS_MSWIN*/ } if ((format_flags & fLOG_Level) != 0 && (call_data->level != eLOG_Note || !(format_flags & fLOG_OmitNoteLevel))) { level_len = strlen(LOG_LevelStr(call_data->level)) + 2; } if ((format_flags & fLOG_Module) != 0 && call_data->module && *call_data->module) { module_len = strlen(call_data->module) + 3; } if ((format_flags & fLOG_FileLine) != 0 && call_data->file && *call_data->file) { file_line_len = 12 + strlen(call_data->file) + 11; } if (call_data->message && *call_data->message) { message_len = strlen(call_data->message); } if ( call_data->raw_size ) { const unsigned char* d = (const unsigned char*) call_data->raw_data; size_t i = call_data->raw_size; for (data_len = 0; i; i--, d++) { if (*d == '\\' || *d == '\r' || *d == '\t') { data_len++; } else if (!isprint(*d) && *d != '\n') { data_len += 2; } } data_len += sizeof(s_RawData_Begin) + 20 + call_data->raw_size + sizeof(s_RawData_End); } /* Allocate memory for the resulting message */ total_len = datetime_len + file_line_len + module_len + level_len + message_len + data_len; str = (char*) malloc(total_len + 1); if ( !str ) { assert(0); return 0; } /* Compose the message */ str[0] = '\0'; if ( datetime_len ) { strcpy(str, datetime); } if ( file_line_len ) { sprintf(str + strlen(str), "\"%s\", line %d: ", call_data->file, (int) call_data->line); } if ( module_len ) { strcat(str, "["); strcat(str, call_data->module); strcat(str, "] "); } if ( level_len ) { strcat(str, LOG_LevelStr(call_data->level)); strcat(str, ": "); } if ( message_len ) { strcat(str, call_data->message); } if ( data_len ) { size_t i; char* s; const unsigned char* d; s = str + strlen(str); sprintf(s, s_RawData_Begin, (unsigned long) call_data->raw_size, call_data->raw_size == 1 ? "" : "s"); s += strlen(s); d = (const unsigned char*) call_data->raw_data; for (i = call_data->raw_size; i; i--, d++) { if (*d == '\\') { *s++ = '\\'; *s++ = '\\'; } else if (*d == '\r') { *s++ = '\\'; *s++ = 'r'; } else if (*d == '\t') { *s++ = '\\'; *s++ = 't'; } else if (!isprint(*d) && *d != '\n') { static const char s_Hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; *s++ = '\\'; *s++ = s_Hex[*d / 16]; *s++ = s_Hex[*d % 16]; } else { *s++ = (char) *d; } } strcpy(s, s_RawData_End); } assert(strlen(str) <= total_len); return str;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -