xdasd_log.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *--------------------------------------------------------------------------*/
00030 
00039 #include "xdasd_log.h"
00040 
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <stdarg.h>
00044 #include <time.h>
00045 
00046 #ifndef _WIN32
00047 # include <sys/types.h>
00048 # include <sys/stat.h>
00049 #endif
00050 
00059 static FILE * g_xdasd_logfile = 0;
00060 
00065 static int g_xdasd_loglevel = 0;
00066 
00073 static void log_time(void)
00074 {
00075    time_t curtime = time(0);
00076    fprintf(g_xdasd_logfile, "[%.*s] ", 24, ctime(&curtime)); 
00077 }
00078 
00082 /*--------------------------------------------------------------------------*/
00083 
00096 int xdasd_log_open(const char * path, int append, int level)
00097 {
00098    g_xdasd_loglevel = level;
00099 
00100    if (g_xdasd_logfile)
00101       fclose(g_xdasd_logfile);
00102 
00103    if (*path == 0)
00104       g_xdasd_logfile = stdout; /* Log to console. */
00105    else
00106    {
00107 #ifndef _WIN32
00108       umask(0077); /* only owner can read/write */
00109 #endif        
00110       if (append)
00111          g_xdasd_logfile = fopen(path, "a");
00112       else
00113          g_xdasd_logfile = fopen(path, "w");
00114 
00115       if (g_xdasd_logfile == 0)
00116          return -1;  /* could not open the log file */
00117    }
00118    return 0;
00119 }
00120 
00125 void xdasd_log_close(void)
00126 {
00127    fclose(g_xdasd_logfile);
00128    g_xdasd_logfile = 0;
00129 }
00130 
00142 int xdasd_log_level(void)
00143 {
00144    static int checked = 0;
00145 
00146    if (!checked)
00147    {
00148       char * llenvstr = getenv("XDASD_LOG_LEVEL");
00149       int loglevel = llenvstr? atoi(llenvstr): 0;
00150       if (loglevel > g_xdasd_loglevel)
00151          g_xdasd_loglevel = loglevel;
00152       checked = 1;
00153    }
00154    return g_xdasd_loglevel;
00155 }
00156 
00169 void xdasd_log(int level, const char * msg, ... )
00170 {
00171    if (g_xdasd_logfile && level <= xdasd_log_level())
00172    {
00173       va_list ap;
00174 
00175       log_time();
00176 
00177       va_start(ap, msg);
00178       vfprintf(g_xdasd_logfile, msg, ap); 
00179       va_end(ap);
00180       fflush(g_xdasd_logfile);
00181    }
00182 }
00183 
00190 void xdasd_fatal(const char * msg, ... )
00191 {
00192    FILE * f = g_xdasd_logfile? g_xdasd_logfile: stderr;
00193    va_list ap;
00194 
00195    log_time();
00196 
00197    fprintf(f, "FATAL: ");
00198    va_start(ap, msg);
00199    vfprintf(f, msg, ap);
00200    va_end(ap);
00201    exit(1);
00202 }
00203 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6