source: XIOS/dev/dev_olga/extern/src_netcdf4/oclog.c @ 1620

Last change on this file since 1620 was 409, checked in by ymipsl, 11 years ago

Add improved nectdf internal library src

YM

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1/* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
2   See the COPYRIGHT file for more information. */
3
4#include "config.h"
5
6#include "ocinternal.h"
7#include <stdio.h>
8#include <fcntl.h>
9
10#define PREFIXLEN 8
11
12#define ENVFLAG "OCLOGFILE"
13
14static int ocloginit = 0;
15static int oclogging = 0;
16static char* oclogfile = NULL;
17static FILE* oclogstream = NULL;
18
19void
20oc_loginit(void)
21{
22    ocloginit = 1;
23    oc_setlogging(0);
24    oclogfile = NULL;
25    oclogstream = NULL;
26    /* Use environment variables to preset oclogging state*/
27    /* I hope this is portable*/
28    if(getenv(ENVFLAG) != NULL) {
29        const char* file = getenv(ENVFLAG);
30        oc_setlogging(1);
31        oc_logopen(file);
32    }
33}
34
35void
36oc_setlogging(int tf)
37{
38    if(!ocloginit) oc_loginit();
39    oclogging = tf;
40}
41
42void
43oc_logopen(const char* file)
44{
45    if(!ocloginit) oc_loginit();
46    if(oclogfile != NULL) {
47        fclose(oclogstream);
48        free(oclogfile);
49        oclogfile = NULL;
50    }
51    if(file == NULL || strlen(file) == 0) {
52        /* use stderr*/
53        oclogstream = stderr;
54        oclogfile = NULL;
55    } else {
56        int fd;
57        oclogfile = (char*)malloc(strlen(file)+1);
58        strcpy(oclogfile,file);
59        oclogstream = NULL;
60        /* We need to deal with this file carefully
61           to avoid unauthorized access*/
62        fd = open(oclogfile,O_WRONLY|O_APPEND|O_CREAT,0600);
63        if(fd >= 0) {
64            oclogstream = fdopen(fd,"a");
65        } else {
66            free(oclogfile);
67            oclogfile = NULL;
68            oc_setlogging(0);
69        }
70    }
71}
72
73void
74oc_logclose(void)
75{
76    if(oclogfile != NULL && oclogstream != NULL) {
77        fclose(oclogstream);
78        oclogstream = NULL;
79        if(oclogfile != NULL) free(oclogfile);
80        oclogfile = NULL;
81    }
82}
83
84void
85oc_log(int tag, const char* fmt, ...)
86{
87    va_list args;
88    char* prefix;
89    if(!oclogging || oclogstream == NULL) return;
90
91    switch (tag) {
92    case LOGWARN: prefix = "Warning:"; break;
93    case LOGERR:  prefix = "Error:  "; break;
94    case LOGNOTE: prefix = "Note:   "; break;
95    case LOGDBG:  prefix = "Debug:  "; break;
96    default:
97        fprintf(oclogstream,"Error:  Bad log prefix: %d\n",tag);
98        prefix = "Error:  ";
99        break;
100    }
101    fprintf(oclogstream,"%s:",prefix);
102
103    if(fmt != NULL) {
104      va_start(args, fmt);
105      vfprintf(oclogstream, fmt, args);
106      va_end( args );
107    }
108    fprintf(oclogstream, "\n" );
109    fflush(oclogstream);
110}
111
112void
113oc_logtext(int tag, const char* text)
114{
115    char line[1024];
116    size_t delta = 0;
117    const char* eol = text;
118
119    if(!oclogging || oclogstream == NULL) return;
120
121    while(*text) {
122        eol = strchr(text,'\n');
123        if(eol == NULL)
124            delta = strlen(text);
125        else
126            delta = (eol - text);
127        if(delta > 0) memcpy(line,text,delta);
128        line[delta] = '\0';
129        fprintf(oclogstream,"        %s\n",line);
130        text = eol+1;
131    }
132}
Note: See TracBrowser for help on using the repository browser.