<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>i3: src/log.c Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <h1>src/log.c</h1><a href="a00070.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * vim:ts=8:expandtab</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * i3 - an improved dynamic tiling window manager</span> <a name="l00005"></a>00005 <span class="comment"> *</span> <a name="l00006"></a>00006 <span class="comment"> * © 2009 Michael Stapelberg and contributors</span> <a name="l00007"></a>00007 <span class="comment"> *</span> <a name="l00008"></a>00008 <span class="comment"> * See file LICENSE for license information.</span> <a name="l00009"></a>00009 <span class="comment"> *</span> <a name="l00010"></a>00010 <span class="comment"> * src/log.c: handles the setting of loglevels, contains the logging functions.</span> <a name="l00011"></a>00011 <span class="comment"> *</span> <a name="l00012"></a>00012 <span class="comment"> */</span> <a name="l00013"></a>00013 <span class="preprocessor">#include <stdarg.h></span> <a name="l00014"></a>00014 <span class="preprocessor">#include <stdio.h></span> <a name="l00015"></a>00015 <span class="preprocessor">#include <string.h></span> <a name="l00016"></a>00016 <span class="preprocessor">#include <stdbool.h></span> <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="a00052.html">util.h</a>"</span> <a name="l00019"></a>00019 <span class="preprocessor">#include "<a class="code" href="a00044.html">log.h</a>"</span> <a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="comment">/* loglevels.h is autogenerated at make time */</span> <a name="l00022"></a>00022 <span class="preprocessor">#include "<a class="code" href="a00045.html">loglevels.h</a>"</span> <a name="l00023"></a>00023 <a name="l00024"></a><a class="code" href="a00070.html#a26c6d742383e1a6bcef226fd6b53161e">00024</a> <span class="keyword">static</span> uint32_t <a class="code" href="a00070.html#a26c6d742383e1a6bcef226fd6b53161e">loglevel</a> = 0; <a name="l00025"></a><a class="code" href="a00070.html#ab3f078684998b83967d507d0f453f454">00025</a> <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="a00070.html#ab3f078684998b83967d507d0f453f454">verbose</a> = <span class="keyword">false</span>; <a name="l00026"></a>00026 <a name="l00033"></a><a class="code" href="a00044.html#a9aeb8f383a036722d9213b750e90703d">00033</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a9aeb8f383a036722d9213b750e90703d" title="Set verbosity of i3.">set_verbosity</a>(<span class="keywordtype">bool</span> _verbose) { <a name="l00034"></a>00034 <a class="code" href="a00070.html#ab3f078684998b83967d507d0f453f454">verbose</a> = _verbose; <a name="l00035"></a>00035 } <a name="l00036"></a>00036 <a name="l00041"></a><a class="code" href="a00044.html#a71ad89f7f5c5c75b215dfa26a9c1bc43">00041</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a71ad89f7f5c5c75b215dfa26a9c1bc43" title="Enables the given loglevel.">add_loglevel</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *level) { <a name="l00042"></a>00042 <span class="comment">/* Handle the special loglevel "all" */</span> <a name="l00043"></a>00043 <span class="keywordflow">if</span> (strcasecmp(level, <span class="stringliteral">"all"</span>) == 0) { <a name="l00044"></a>00044 <a class="code" href="a00070.html#a26c6d742383e1a6bcef226fd6b53161e">loglevel</a> = <a class="code" href="a00058.html#ab5eb23180f7cc12b7d6c04a8ec067fdd">UINT32_MAX</a>; <a name="l00045"></a>00045 <span class="keywordflow">return</span>; <a name="l00046"></a>00046 } <a name="l00047"></a>00047 <a name="l00048"></a>00048 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <span class="keyword">sizeof</span>(<a class="code" href="a00044.html#aea3c9cd7c28ac0ebbc7473771751ed92">loglevels</a>) / <span class="keyword">sizeof</span>(<span class="keywordtype">char</span>*); i++) { <a name="l00049"></a>00049 <span class="keywordflow">if</span> (strcasecmp(<a class="code" href="a00044.html#aea3c9cd7c28ac0ebbc7473771751ed92">loglevels</a>[i], level) != 0) <a name="l00050"></a>00050 <span class="keywordflow">continue</span>; <a name="l00051"></a>00051 <a name="l00052"></a>00052 <span class="comment">/* The position in the array (plus one) is the amount of times</span> <a name="l00053"></a>00053 <span class="comment"> * which we need to shift 1 to the left to get our bitmask for</span> <a name="l00054"></a>00054 <span class="comment"> * the specific loglevel. */</span> <a name="l00055"></a>00055 <a class="code" href="a00070.html#a26c6d742383e1a6bcef226fd6b53161e">loglevel</a> |= (1 << (i+1)); <a name="l00056"></a>00056 <span class="keywordflow">break</span>; <a name="l00057"></a>00057 } <a name="l00058"></a>00058 } <a name="l00059"></a>00059 <a name="l00060"></a>00060 <span class="comment">/*</span> <a name="l00061"></a>00061 <span class="comment"> * Logs the given message to stdout while prefixing the current time to it.</span> <a name="l00062"></a>00062 <span class="comment"> * This is to be called by *LOG() which includes filename/linenumber/function.</span> <a name="l00063"></a>00063 <span class="comment"> *</span> <a name="l00064"></a>00064 <span class="comment"> */</span> <a name="l00065"></a><a class="code" href="a00070.html#a1740300779c879a1c03fa2987f25c111">00065</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a1740300779c879a1c03fa2987f25c111">vlog</a>(<span class="keywordtype">char</span> *fmt, va_list args) { <a name="l00066"></a>00066 <span class="keywordtype">char</span> timebuf[64]; <a name="l00067"></a>00067 <a name="l00068"></a>00068 <span class="comment">/* Get current time */</span> <a name="l00069"></a>00069 time_t t = time(NULL); <a name="l00070"></a>00070 <span class="comment">/* Convert time to local time (determined by the locale) */</span> <a name="l00071"></a>00071 <span class="keyword">struct </span>tm *tmp = localtime(&t); <a name="l00072"></a>00072 <span class="comment">/* Generate time prefix */</span> <a name="l00073"></a>00073 strftime(timebuf, <span class="keyword">sizeof</span>(timebuf), <span class="stringliteral">"%x %X - "</span>, tmp); <a name="l00074"></a>00074 printf(<span class="stringliteral">"%s"</span>, timebuf); <a name="l00075"></a>00075 vprintf(fmt, args); <a name="l00076"></a>00076 } <a name="l00077"></a>00077 <a name="l00083"></a><a class="code" href="a00044.html#a8e31d2a541996fabe1a0f7086886f9b1">00083</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a8e31d2a541996fabe1a0f7086886f9b1" title="Logs the given message to stdout while prefixing the current time to it, but only...">verboselog</a>(<span class="keywordtype">char</span> *fmt, ...) { <a name="l00084"></a>00084 va_list args; <a name="l00085"></a>00085 <a name="l00086"></a>00086 <span class="keywordflow">if</span> (!<a class="code" href="a00070.html#ab3f078684998b83967d507d0f453f454">verbose</a>) <a name="l00087"></a>00087 <span class="keywordflow">return</span>; <a name="l00088"></a>00088 <a name="l00089"></a>00089 va_start(args, fmt); <a name="l00090"></a>00090 <a class="code" href="a00070.html#a1740300779c879a1c03fa2987f25c111">vlog</a>(fmt, args); <a name="l00091"></a>00091 va_end(args); <a name="l00092"></a>00092 } <a name="l00093"></a>00093 <a name="l00098"></a><a class="code" href="a00044.html#a04ee816a9140e8e8bbd4b1892e25f6ef">00098</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a04ee816a9140e8e8bbd4b1892e25f6ef" title="Logs the given message to stdout while prefixing the current time to it.">errorlog</a>(<span class="keywordtype">char</span> *fmt, ...) { <a name="l00099"></a>00099 va_list args; <a name="l00100"></a>00100 <a name="l00101"></a>00101 va_start(args, fmt); <a name="l00102"></a>00102 <a class="code" href="a00070.html#a1740300779c879a1c03fa2987f25c111">vlog</a>(fmt, args); <a name="l00103"></a>00103 va_end(args); <a name="l00104"></a>00104 } <a name="l00105"></a>00105 <a name="l00106"></a>00106 <span class="comment">/*</span> <a name="l00107"></a>00107 <span class="comment"> * Logs the given message to stdout while prefixing the current time to it,</span> <a name="l00108"></a>00108 <span class="comment"> * but only if the corresponding debug loglevel was activated.</span> <a name="l00109"></a>00109 <span class="comment"> * This is to be called by DLOG() which includes filename/linenumber</span> <a name="l00110"></a>00110 <span class="comment"> *</span> <a name="l00111"></a>00111 <span class="comment"> */</span> <a name="l00112"></a><a class="code" href="a00044.html#a04d84deb5a91e34d07c90e99cc8a35bd">00112</a> <span class="keywordtype">void</span> <a class="code" href="a00070.html#a04d84deb5a91e34d07c90e99cc8a35bd" title="Logs the given message to stdout while prefixing the current time to it, but only...">debuglog</a>(<span class="keywordtype">int</span> lev, <span class="keywordtype">char</span> *fmt, ...) { <a name="l00113"></a>00113 va_list args; <a name="l00114"></a>00114 <a name="l00115"></a>00115 <span class="keywordflow">if</span> ((<a class="code" href="a00070.html#a26c6d742383e1a6bcef226fd6b53161e">loglevel</a> & lev) == 0) <a name="l00116"></a>00116 <span class="keywordflow">return</span>; <a name="l00117"></a>00117 <a name="l00118"></a>00118 va_start(args, fmt); <a name="l00119"></a>00119 <a class="code" href="a00070.html#a1740300779c879a1c03fa2987f25c111">vlog</a>(fmt, args); <a name="l00120"></a>00120 va_end(args); <a name="l00121"></a>00121 } </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>