📄 slpd_log.c
字号:
/***************************************************************************//* *//* Project: OpenSLP - OpenSource implementation of Service Location *//* Protocol Version 2 *//* *//* File: slpd_log.c *//* *//* Abstract: slpd logging functions *//* *//* WARNING: NOT thread safe! *//* *//*-------------------------------------------------------------------------*//* *//* Please submit patches to http://www.openslp.org *//* *//*-------------------------------------------------------------------------*//* *//* Copyright (C) 2000 Caldera Systems, Inc *//* All rights reserved. *//* *//* Redistribution and use in source and binary forms, with or without *//* modification, are permitted provided that the following conditions are *//* met: */ /* *//* Redistributions of source code must retain the above copyright *//* notice, this list of conditions and the following disclaimer. *//* *//* 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. *//* *//* Neither the name of Caldera 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 CALDERA *//* SYSTEMS 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. *//* *//***************************************************************************//*=========================================================================*//* slpd includes *//*=========================================================================*/#include "slpd_log.h"#include "slpd_property.h"#include <time.h>/********************************************//* TODO: Make these functions thread safe!! *//********************************************//*=========================================================================*/static FILE* G_SlpdLogFile = 0;/*=========================================================================*//*=========================================================================*/int SLPDLogFileOpen(const char* path, int append) /* Prepares the file at the specified path as the log file. *//* *//* path - (IN) the path to the log file. If path is the empty string *//* (""), then we log to stdout. *//* *//* append - (IN) if zero log file will be truncated. *//* *//* Returns - zero on success. errno on failure. *//*=========================================================================*/{ if (G_SlpdLogFile) { /* logfile was already open close it */ fclose(G_SlpdLogFile); } if (*path == 0) { /* Log to console. */ G_SlpdLogFile = stdout; } else { /* Log to file. */#ifndef _WIN32 /* only owner can read/write */ umask(0077); #endif if (append) { G_SlpdLogFile = fopen(path,"a"); } else { G_SlpdLogFile = fopen(path,"w"); } if (G_SlpdLogFile == 0) { /* could not open the log file */ return -1; } } return 0;}#ifdef DEBUG/*=========================================================================*/int SLPDLogFileClose()/* Releases resources associated with the log file *//*=========================================================================*/{ fclose(G_SlpdLogFile); return 0;}#endif/*=========================================================================*/void SLPDLog(const char* msg, ...)/* Logs a message *//*=========================================================================*/{ va_list ap; if (G_SlpdLogFile) { va_start(ap,msg); vfprintf(G_SlpdLogFile,msg,ap); va_end(ap); fflush(G_SlpdLogFile); }}/*=========================================================================*/void SLPDFatal(const char* msg, ...)/* Logs a message and halts the process *//*=========================================================================*/{ va_list ap; if (G_SlpdLogFile) { fprintf(G_SlpdLogFile,"A FATAL Error has occured:\n"); va_start(ap,msg); vfprintf(G_SlpdLogFile,msg,ap); va_end(ap); fflush(G_SlpdLogFile); } else { fprintf(stderr,"A FATAL Error has occured:\n"); va_start(ap,msg); vprintf(msg,ap); va_end(ap); } exit(1);}/*=========================================================================*/void SLPDLogBuffer(const char* prefix, int bufsize, const char* buf)/* Writes a buffer to the logfile *//*=========================================================================*/{ if (G_SlpdLogFile) { fprintf(G_SlpdLogFile,"%s",prefix); fwrite(buf,bufsize,1,G_SlpdLogFile); fprintf(G_SlpdLogFile,"\n"); fflush(G_SlpdLogFile); }}/*=========================================================================*/void SLPDLogTime()/* Logs a timestamp *//*=========================================================================*/{ time_t curtime = time(NULL); SLPDLog("%s",ctime(&curtime)); }/*-------------------------------------------------------------------------*/void SLPDLogSrvRqstMessage(SLPSrvRqst* srvrqst)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVRQST:\n"); SLPDLogBuffer(" srvtype = ", srvrqst->srvtypelen, srvrqst->srvtype); SLPDLogBuffer(" scopelist = ", srvrqst->scopelistlen, srvrqst->scopelist); SLPDLogBuffer(" predicate = ", srvrqst->predicatelen, srvrqst->predicate);}/*-------------------------------------------------------------------------*/void SLPDLogSrvRplyMessage(SLPSrvRply* srvrply)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVRPLY:\n"); SLPDLog(" errorcode = %i\n",srvrply->errorcode);}/*-------------------------------------------------------------------------*/void SLPDLogSrvRegMessage(SLPSrvReg* srvreg)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVREG:\n"); SLPDLogBuffer(" srvtype = ", srvreg->srvtypelen, srvreg->srvtype); SLPDLogBuffer(" scope = ", srvreg->scopelistlen, srvreg->scopelist); SLPDLogBuffer(" url = ", srvreg->urlentry.urllen, srvreg->urlentry.url); SLPDLogBuffer(" attributes = ", srvreg->attrlistlen, srvreg->attrlist);}/*-------------------------------------------------------------------------*/void SLPDLogSrvDeRegMessage(SLPSrvDeReg* srvdereg)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVDEREG:\n"); SLPDLogBuffer(" scope = ", srvdereg->scopelistlen, srvdereg->scopelist); SLPDLogBuffer(" url = ", srvdereg->urlentry.urllen, srvdereg->urlentry.url);}/*-------------------------------------------------------------------------*/void SLPDLogSrvAckMessage(SLPSrvAck* srvack)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVACK:\n"); SLPDLog(" errorcode = %i\n",srvack->errorcode);}/*-------------------------------------------------------------------------*/void SLPDLogAttrRqstMessage(SLPAttrRqst* attrrqst)/*-------------------------------------------------------------------------*/{ SLPDLog("Message ATTRRQST:\n"); SLPDLogBuffer(" scope = ", attrrqst->scopelistlen, attrrqst->scopelist); SLPDLogBuffer(" url = ", attrrqst->urllen, attrrqst->url);}/*-------------------------------------------------------------------------*/void SLPDLogAttrRplyMessage(SLPAttrRply* attrrply)/*-------------------------------------------------------------------------*/{ SLPDLog("Message ATTRRPLY:\n"); SLPDLog(" errorcode = %i\n",attrrply->errorcode);} /*-------------------------------------------------------------------------*/void SLPDLogDAAdvertMessage(SLPDAAdvert* daadvert)/*-------------------------------------------------------------------------*/{ SLPDLog("Message DAADVERT:\n"); SLPDLogBuffer(" scope = ", daadvert->scopelistlen, daadvert->scopelist); SLPDLogBuffer(" url = ", daadvert->urllen, daadvert->url); SLPDLogBuffer(" attributes = ", daadvert->attrlistlen, daadvert->attrlist);}/*-------------------------------------------------------------------------*/void SLPDLogSrvTypeRqstMessage(SLPSrvTypeRqst* srvtyperqst)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SRVTYPERQST:\n"); SLPDLogBuffer(" namingauth = ", srvtyperqst->namingauthlen, srvtyperqst->namingauth); SLPDLogBuffer(" scope = ", srvtyperqst->scopelistlen, srvtyperqst->scopelist);}/*-------------------------------------------------------------------------*/void SLPDLogSrvTypeRplyMessage(SLPSrvTypeRply* srvtyperply)/*-------------------------------------------------------------------------*/ { SLPDLog("Message SRVTYPERPLY:\n"); SLPDLog(" errorcode = %i\n",srvtyperply->errorcode);} /*-------------------------------------------------------------------------*/void SLPDLogSAAdvertMessage(SLPSAAdvert* saadvert)/*-------------------------------------------------------------------------*/{ SLPDLog("Message SAADVERT:\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -