]> sourceware.org Git - systemtap.git/blob - runtime/io.c
1483acfdaf60f70b4ad42524f93f10bfb8a68790
[systemtap.git] / runtime / io.c
1 /* -*- linux-c -*-
2 * I/O for printing warnings, errors and debug messages
3 * Copyright (C) 2005, 2006, 2007 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef _IO_C_
12 #define _IO_C_
13
14 #include "transport/transport.c"
15
16 void _stp_print_flush (void);
17 void _stp_string_cat_cstr (String str1, const char *str2);
18
19 /** @file io.c
20 * @brief I/O for printing warnings, errors and debug messages.
21 */
22 /** @addtogroup io I/O
23 * @{
24 */
25
26 #define WARN_STRING "WARNING: "
27 #define ERR_STRING "ERROR: "
28 enum code { INFO=0, WARN, ERROR, DBUG };
29
30 /** private buffer for _stp_log() */
31 #define STP_LOG_BUF_LEN 256
32
33 typedef char _stp_lbuf[STP_LOG_BUF_LEN];
34 void *Stp_lbuf = NULL;
35
36 static void _stp_vlog (enum code type, const char *func, int line, const char *fmt, va_list args)
37 {
38 int num;
39 char *buf = per_cpu_ptr(Stp_lbuf, smp_processor_id());
40 int start = 0;
41
42 if (type == DBUG) {
43 start = _stp_snprintf(buf, STP_LOG_BUF_LEN, "\033[36m%s:%d:\033[0m ", func, line);
44 } else if (type == WARN) {
45 strcpy (buf, WARN_STRING);
46 start = sizeof(WARN_STRING) - 1;
47 } else if (type == ERROR) {
48 strcpy (buf, ERR_STRING);
49 start = sizeof(ERR_STRING) - 1;
50 }
51
52 num = _stp_vscnprintf (buf + start, STP_LOG_BUF_LEN - start - 1, fmt, args);
53 if (num + start) {
54 if (buf[num + start - 1] != '\n') {
55 buf[num + start] = '\n';
56 num++;
57 buf[num + start] = '\0';
58 }
59
60 if (type != DBUG)
61 _stp_write(STP_OOB_DATA, buf, start + num + 1);
62 else {
63 _stp_string_cat_cstr(_stp_stdout,buf);
64 _stp_print_flush();
65 }
66 }
67 }
68
69 /** Logs Data.
70 * This function sends the message immediately to staprun. It
71 * will also be sent over the bulk transport (relayfs) if it is
72 * being used. If the last character is not a newline, then one
73 * is added. This function is not as efficient as _stp_printf()
74 * and should only be used for urgent messages. You probably want
75 * dbug(), or _stp_warn().
76 * @param fmt A variable number of args.
77 * @todo Evaluate if this function is necessary.
78 */
79 void _stp_log (const char *fmt, ...)
80 {
81 va_list args;
82 va_start(args, fmt);
83 _stp_vlog (INFO, NULL, 0, fmt, args);
84 va_end(args);
85 }
86
87 /** Prints warning.
88 * This function sends a warning message immediately to staprun. It
89 * will also be sent over the bulk transport (relayfs) if it is
90 * being used. If the last character is not a newline, then one
91 * is added.
92 * @param fmt A variable number of args.
93 */
94 void _stp_warn (const char *fmt, ...)
95 {
96 va_list args;
97 va_start(args, fmt);
98 _stp_vlog (WARN, NULL, 0, fmt, args);
99 va_end(args);
100 }
101
102 /** Exits and unloads the module.
103 * This function sends a signal to staprun to tell it to
104 * unload the module and exit. The module will not be
105 * unloaded until after the current probe returns.
106 * @note Be careful to not treat this like the Linux exit()
107 * call. You should probably call return immediately after
108 * calling _stp_exit().
109 */
110 void _stp_exit (void)
111 {
112 _stp_exit_flag = 1;
113 }
114
115 /** Prints error message and exits.
116 * This function sends an error message immediately to staprun. It
117 * will also be sent over the bulk transport (relayfs) if it is
118 * being used. If the last character is not a newline, then one
119 * is added.
120 *
121 * After the error message is displayed, the module will be unloaded.
122 * @param fmt A variable number of args.
123 * @sa _stp_exit().
124 */
125 void _stp_error (const char *fmt, ...)
126 {
127 va_list args;
128 va_start(args, fmt);
129 _stp_vlog (ERROR, NULL, 0, fmt, args);
130 va_end(args);
131 _stp_exit();
132 }
133
134
135 /** Prints error message.
136 * This function sends an error message immediately to staprun. It
137 * will also be sent over the bulk transport (relayfs) if it is
138 * being used. If the last character is not a newline, then one
139 * is added.
140 *
141 * @param fmt A variable number of args.
142 * @sa _stp_error
143 */
144 void _stp_softerror (const char *fmt, ...)
145 {
146 va_list args;
147 va_start(args, fmt);
148 _stp_vlog (ERROR, NULL, 0, fmt, args);
149 va_end(args);
150 }
151
152
153 static void _stp_dbug (const char *func, int line, const char *fmt, ...)
154 {
155 va_list args;
156 va_start(args, fmt);
157 _stp_vlog (DBUG, func, line, fmt, args);
158 va_end(args);
159 }
160
161 /** @} */
162 #endif /* _IO_C_ */
This page took 0.044685 seconds and 4 git commands to generate.