File: logging.cc

package info (click to toggle)
dnprogs 2.52
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,644 kB
  • ctags: 4,021
  • sloc: ansic: 26,737; cpp: 10,666; makefile: 832; sh: 537; awk: 13
file content (101 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/******************************************************************************
    logging.cc from libdap

    Copyright (C) 1999 Christine Caulfield       christine.caulfield@googlemail.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Logging module for libdap
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include "logging.h"

static enum {DAPLOG_MONO, DAPLOG_STDERR, DAPLOG_SYSLOG} 
       log_type = DAPLOG_SYSLOG;

static bool show_pid = false;

void init_logging(const char *progname, char type, bool pid)
{
    switch (type)
    {
    case 'm': 
	log_type = DAPLOG_MONO;
	break;
    case 'e': 
	log_type = DAPLOG_STDERR;
	break;
    default:
	log_type = DAPLOG_SYSLOG;
	openlog(progname, LOG_PID, LOG_DAEMON);
	break;
    }
#ifndef NO_FORK
    show_pid = pid;
#endif
}

static void daplog_stderr(int level, const char *fmt, va_list ap)
{

    if (show_pid) fprintf(stderr, "[%d] ", getpid());

    vfprintf(stderr, fmt, ap);
}


// This will output to the /dev/mono device (using my mono driver - see
// web page for details) or tty13 which is usually the mono monitor
// in a framebuffer system.
static void daplog_mono(int level, const char *fmt, va_list ap)
{
    char        outbuf[4096];
    static int  fd = 0;

    if (!fd) fd = open("/dev/mono", O_WRONLY);
    if (!fd) fd = open("/dev/tty13", O_WRONLY);

#ifndef NO_FORK
    sprintf(outbuf, "[%d] ", getpid());
    write(fd, outbuf, strlen(outbuf));
#endif

    vsprintf(outbuf, fmt, ap);
    write(fd, outbuf, strlen(outbuf));
}

void daplog(int level, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);

    switch(log_type)
    {
    case DAPLOG_MONO:
	daplog_mono(level, fmt, ap);
	break;
    case DAPLOG_STDERR:
	daplog_stderr(level, fmt, ap);
	break;
    case DAPLOG_SYSLOG:
	vsyslog(level, fmt, ap);
	break;
    }
    va_end(ap);
}