~ubuntu-branches/ubuntu/warty/xmedcon/warty

« back to all changes in this revision

Viewing changes to libs/dicom/log.c

  • Committer: Bazaar Package Importer
  • Author(s): Roland Marcus Rutschmann
  • Date: 2004-06-07 09:00:14 UTC
  • Revision ID: james.westby@ubuntu.com-20040607090014-t39n52qc9zjqqqkh
Tags: upstream-0.9.6
ImportĀ upstreamĀ versionĀ 0.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************
 
2
 * libdicom by Tony Voet *
 
3
 *************************/
 
4
/* 
 
5
 * $Id: log.c,v 1.2 2002/11/06 23:31:16 enlf Exp $
 
6
 */
 
7
 
 
8
#include <stdio.h>
 
9
#include <string.h>
 
10
#include <unistd.h>
 
11
#include <time.h>
 
12
#include "dicom.h"
 
13
 
 
14
CONDITION dicom_log_level=NOTICE;
 
15
 
 
16
/* eNlf: BEGIN -- change for compilation error on Red Hat 6.0 */
 
17
/* static FILE     *stream=stderr;                            */
 
18
/* The above statement fails: initializer not constant        */
 
19
/* eNlf: END   -- change for compilation error on Red Hat 6.0 */
 
20
static FILE     *stream=NULL;
 
21
static char     *program=NULL;
 
22
 
 
23
/************
 
24
 * log name *
 
25
 ************/
 
26
 
 
27
void dicom_log_name(char *name)
 
28
{
 
29
  program=strrchr(name,'/');
 
30
 
 
31
  if (program)
 
32
    program++;
 
33
  else
 
34
    program=name;
 
35
}
 
36
 
 
37
/************
 
38
 * log open *
 
39
 ************/
 
40
 
 
41
int dicom_log_open(const char *file)
 
42
{
 
43
  if (!file)
 
44
  {
 
45
    dicom_log(ERROR,"No file given");
 
46
    return -1;
 
47
  }
 
48
 
 
49
  stream=fopen(file,"a");
 
50
 
 
51
  if (!stream)
 
52
  {
 
53
    stream=stderr;
 
54
    dicom_log(ERROR,"Unable to open log file");
 
55
    return -1;
 
56
  }
 
57
 
 
58
  return 0;
 
59
}
 
60
 
 
61
/*******
 
62
 * log *
 
63
 *******/
 
64
 
 
65
void dicom_log(CONDITION condition,const char *message)
 
66
{
 
67
  time_t        t;
 
68
  char          tmp[32];
 
69
 
 
70
  static char *explination[]=
 
71
  {
 
72
    "emergency",
 
73
    "alert",
 
74
    "critical",
 
75
    "error",
 
76
    "warning",
 
77
    "notice",
 
78
    "info",
 
79
    "debug"
 
80
  };
 
81
 
 
82
  if (condition>dicom_log_level)
 
83
    return;
 
84
 
 
85
  time(&t);
 
86
  strftime(tmp,32,"%b %d %H:%M:%S",localtime(&t));
 
87
 
 
88
/* eNlf: BEGIN  -- change for compilation error on Red Hat 6.0 */
 
89
  if (stream == NULL) {
 
90
    fprintf(stderr,"%s %s[%u]: %s: %s\n",
 
91
      tmp,
 
92
      program ? program : "log",
 
93
      (unsigned int) getpid(),
 
94
      explination[condition],
 
95
      message);
 
96
  }else{
 
97
    fprintf(stream,"%s %s[%u]: %s: %s\n",
 
98
      tmp,
 
99
      program ? program : "log",
 
100
      (unsigned int) getpid(),
 
101
      explination[condition],
 
102
      message);
 
103
  }
 
104
/* eNlf: END   -- change for compilation error on Red Hat 6.0 */
 
105
}
 
106
 
 
107
/*************
 
108
 * log close *
 
109
 *************/
 
110
 
 
111
int dicom_log_close(void)
 
112
{
 
113
  if (stream==stderr)
 
114
  {
 
115
    dicom_log(NOTICE,"Attempt to close stderr");
 
116
    return -1;
 
117
  }
 
118
 
 
119
  if (fclose(stream))
 
120
  {
 
121
    stream=stderr;
 
122
    dicom_log(WARNING,"Unable to close log");
 
123
    return -2;
 
124
  }
 
125
 
 
126
  stream=stderr;
 
127
 
 
128
  return 0;
 
129
}