wimax-tools 1.4.4

/home/users/builder/rpm/BUILD/wimax-tools-1.4.4/include/wimaxll/log.h

Go to the documentation of this file.
00001 /*
00002  * Linux WiMax
00003  * Simple log helpers
00004  *
00005  *
00006  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
00007  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above copyright
00016  *     notice, this list of conditions and the following disclaimer in
00017  *     the documentation and/or other materials provided with the
00018  *     distribution.
00019  *   * Neither the name of Intel Corporation nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 /**
00036  *
00037  * \defgroup helper_log Set of simple log helpers
00038  *
00039  * Log messages to stdout/stderr, with simple log level management.
00040  *
00041  * If the log level is W_PRINT, we assume is a normal message that the
00042  * user wants to see and send it to stdout. If it is any other,
00043  * evaluate if it should be printed based on the current level and
00044  * then print it to stderr.
00045  *
00046  * Before including, W_VERBOSITY must be defined to something that
00047  * yields a numeric value out of of \e { enum w_levels }. When any of
00048  * the w_*() functions/macros is called, if the level it is called
00049  * with is less or equal than the current level defied by W_VERBOSITY,
00050  * it'll be ran, if not, it'll be ignored:
00051  *
00052  * @code
00053  *
00054  * #define W_VERBOSITY W_INFO (or myglobalstruct.verbosity)
00055  * #include <wimaxll/log.h>
00056  *
00057  * somefunc()
00058  * {
00059  *        ...
00060  *        w_d1("debug message\n");
00061  *        ...
00062  * }
00063  *
00064  * @endcode
00065  *
00066  * To control where the log/progress messages go and how they are
00067  * formatted, the client can set a couple of function pointers
00068  * wimaxll_msg_hdr_cb() (which controls how a header/prefix for the
00069  * message is created) and wimaxll_vlmsg_cb(), which takes the message
00070  * and delivers it to whichever destination.
00071  *
00072  * The default implementations are wimaxll_msg_hdr_default() and
00073  * wimaxll_vlmsg_default(), which add a
00074  * "libwimall[DEVICENAME]:" header (with an optional "(@
00075  * FUNCTION:LINE)") and deliver the message to \e stdout if it is a
00076  * normal message (\e W_PRINT) or else if it is an error, warning,
00077  * info or debug message, it is sent to \e stderr.
00078  */
00079 
00080 #ifndef __wimaxll__log_h__
00081 #define __wimaxll__log_h__
00082 
00083 #include <stdio.h>
00084 #include <stdarg.h>
00085 
00086 #ifndef W_VERBOSITY
00087 #error Please #define W_VERBOSITY before including this file
00088 #endif
00089 
00090 /* Logging / printing */
00091 enum w_levels {
00092         W_ERROR,
00093         W_WARN,
00094         W_INFO,
00095         W_PRINT,
00096         W_D0,
00097         W_D1,
00098         W_D2,
00099         W_D3,
00100         W_D4,
00101         W_D5,
00102         W_D6,
00103         W_D7,
00104 };
00105 
00106 struct wimaxll_handle;
00107 
00108 void wimaxll_msg(struct wimaxll_handle *, const char *fmt, ...)
00109         __attribute__ ((format(printf, 2, 3)));
00110 
00111 void wimaxll_lmsg(unsigned level, unsigned current_level,
00112                   const char *origin_str, unsigned origin_line,
00113                   struct wimaxll_handle *wmx, const char *fmt, ...)
00114         __attribute__ ((format(printf, 6, 7)));
00115 
00116 extern void (*wimaxll_vlmsg_cb)(struct wimaxll_handle *, unsigned,
00117                                 const char *, const char *, va_list);
00118 void wimaxll_vlmsg_stderr(struct wimaxll_handle *, unsigned,
00119                           const char *, const char *, va_list);
00120 
00121 extern void (*wimaxll_msg_hdr_cb)(char *, size_t, struct wimaxll_handle *,
00122                                   enum w_levels, const char *, unsigned);
00123 void wimaxll_msg_hdr_default(char *, size_t, struct wimaxll_handle *,
00124                              enum w_levels, const char *, unsigned);
00125 
00126 void w_abort(int result, const char *fmt, ...);
00127 
00128 #define w_error(fmt...) wimaxll_lmsg(W_ERROR, W_VERBOSITY, __func__, __LINE__, NULL, "E: " fmt)
00129 #define w_warn(fmt...) wimaxll_lmsg(W_WARN, W_VERBOSITY, __func__, __LINE__, NULL, "W: " fmt)
00130 #define w_info(fmt...) wimaxll_lmsg(W_INFO, W_VERBOSITY, __func__, __LINE__, NULL, "I: " fmt)
00131 #define w_print(fmt...) wimaxll_lmsg(W_PRINT, W_VERBOSITY, __func__, __LINE__, NULL, fmt)
00132 #define w_d0(fmt...) wimaxll_lmsg(W_D0, W_VERBOSITY, __func__, __LINE__, NULL, "D0: " fmt)
00133 #define w_d1(fmt...) wimaxll_lmsg(W_D1, W_VERBOSITY, __func__, __LINE__, NULL, "D1: " fmt)
00134 #define w_d2(fmt...) wimaxll_lmsg(W_D2, W_VERBOSITY, __func__, __LINE__, NULL, "D2: " fmt)
00135 #define w_d3(fmt...) wimaxll_lmsg(W_D3, W_VERBOSITY, __func__, __LINE__, NULL, "D3: " fmt)
00136 #define w_d4(fmt...) wimaxll_lmsg(W_D4, W_VERBOSITY, __func__, __LINE__, NULL, "D4: " fmt)
00137 #define w_d5(fmt...) wimaxll_lmsg(W_D5, W_VERBOSITY, __func__, __LINE__, NULL, "D5: " fmt)
00138 #define w_d6(fmt...) wimaxll_lmsg(W_D6, W_VERBOSITY, __func__, __LINE__, NULL, "D6: " fmt)
00139 #define w_d7(fmt...) wimaxll_lmsg(W_D7, W_VERBOSITY, __func__, __LINE__, NULL, "D7: " fmt)
00140 
00141 #endif /* #define __wimaxll__log_h__ */