~ubuntu-branches/ubuntu/edgy/libcdio/edgy-updates

« back to all changes in this revision

Viewing changes to lib/driver/logging.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-11-15 16:53:23 UTC
  • mfrom: (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20051115165323-peroku75syl2j36u
Tags: 0.76-1ubuntu1
* Sync to new Debian version, manually apply Ubuntu patches:
  - debian/control: Remove dpkg-awk build dependency.
  - debian/rules: hardcode $LIBCDEV. This keeps the diff small (compared to
    the original patch of changing every ${libcdev} occurence).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    $Id: logging.c,v 1.1 2004/12/18 17:29:32 rocky Exp $
 
3
 
 
4
    Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
 
5
    Copyright (C) 2003, 2004 Rocky Bernstein <rocky@panix.com>
 
6
 
 
7
    This program is free software; you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation; either version 2 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
    GNU General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU General Public License
 
18
    along with this program; if not, write to the Free Software
 
19
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
*/
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include "config.h"
 
24
#endif
 
25
 
 
26
#include <stdlib.h>
 
27
#include <stdarg.h>
 
28
#include <stdio.h>
 
29
 
 
30
#include <cdio/logging.h>
 
31
#include "cdio_assert.h"
 
32
#include "portable.h"
 
33
 
 
34
static const char _rcsid[] = "$Id: logging.c,v 1.1 2004/12/18 17:29:32 rocky Exp $";
 
35
 
 
36
cdio_log_level_t cdio_loglevel_default = CDIO_LOG_WARN;
 
37
 
 
38
static void
 
39
default_cdio_log_handler (cdio_log_level_t level, const char message[])
 
40
{
 
41
  switch (level)
 
42
    {
 
43
    case CDIO_LOG_ERROR:
 
44
      if (level >= cdio_loglevel_default) {
 
45
        fprintf (stderr, "**ERROR: %s\n", message);
 
46
        fflush (stderr);
 
47
      }
 
48
      exit (EXIT_FAILURE);
 
49
      break;
 
50
    case CDIO_LOG_DEBUG:
 
51
      if (level >= cdio_loglevel_default) {
 
52
        fprintf (stdout, "--DEBUG: %s\n", message);
 
53
      }
 
54
      break;
 
55
    case CDIO_LOG_WARN:
 
56
      if (level >= cdio_loglevel_default) {
 
57
        fprintf (stdout, "++ WARN: %s\n", message);
 
58
      }
 
59
      break;
 
60
    case CDIO_LOG_INFO:
 
61
      if (level >= cdio_loglevel_default) {
 
62
        fprintf (stdout, "   INFO: %s\n", message);
 
63
      }
 
64
      break;
 
65
    case CDIO_LOG_ASSERT:
 
66
      if (level >= cdio_loglevel_default) {
 
67
        fprintf (stderr, "!ASSERT: %s\n", message);
 
68
        fflush (stderr);
 
69
      }
 
70
      abort ();
 
71
      break;
 
72
    default:
 
73
      cdio_assert_not_reached ();
 
74
      break;
 
75
    }
 
76
 
 
77
  fflush (stdout);
 
78
}
 
79
 
 
80
static cdio_log_handler_t _handler = default_cdio_log_handler;
 
81
 
 
82
cdio_log_handler_t
 
83
cdio_log_set_handler (cdio_log_handler_t new_handler)
 
84
{
 
85
  cdio_log_handler_t old_handler = _handler;
 
86
 
 
87
  _handler = new_handler;
 
88
 
 
89
  return old_handler;
 
90
}
 
91
 
 
92
static void
 
93
cdio_logv (cdio_log_level_t level, const char format[], va_list args)
 
94
{
 
95
  char buf[1024] = { 0, };
 
96
  static int in_recursion = 0;
 
97
 
 
98
  if (in_recursion)
 
99
    cdio_assert_not_reached ();
 
100
 
 
101
  in_recursion = 1;
 
102
  
 
103
  vsnprintf(buf, sizeof(buf)-1, format, args);
 
104
 
 
105
  _handler(level, buf);
 
106
 
 
107
  in_recursion = 0;
 
108
}
 
109
 
 
110
void
 
111
cdio_log (cdio_log_level_t level, const char format[], ...)
 
112
{
 
113
  va_list args;
 
114
  va_start (args, format);
 
115
  cdio_logv (level, format, args);
 
116
  va_end (args);
 
117
}
 
118
 
 
119
#define CDIO_LOG_TEMPLATE(level, LEVEL) \
 
120
void \
 
121
cdio_ ## level (const char format[], ...) \
 
122
{ \
 
123
  va_list args; \
 
124
  va_start (args, format); \
 
125
  cdio_logv (CDIO_LOG_ ## LEVEL, format, args); \
 
126
  va_end (args); \
 
127
 
128
 
 
129
CDIO_LOG_TEMPLATE(debug, DEBUG)
 
130
CDIO_LOG_TEMPLATE(info, INFO)
 
131
CDIO_LOG_TEMPLATE(warn, WARN)
 
132
CDIO_LOG_TEMPLATE(error, ERROR)
 
133
 
 
134
#undef CDIO_LOG_TEMPLATE
 
135
 
 
136
 
 
137
/* 
 
138
 * Local variables:
 
139
 *  c-file-style: "gnu"
 
140
 *  tab-width: 8
 
141
 *  indent-tabs-mode: nil
 
142
 * End:
 
143
 */