1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2016 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LOG_H
21 #define LOG_H
22 
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include "Ref.h"
27 #include "ResMgr.h"
28 
29 class Log : public ResClient
30 {
31    const char *name;
32    int output;
33    bool need_close_output;
34    bool tty;
35    bool show_pid;
36    bool show_time;
37    bool show_context;
38    bool at_line_start;
39    typedef void (*tty_cb_t)();
40    tty_cb_t tty_cb;
41 
CloseOutput()42    void CloseOutput()
43       {
44 	 if(need_close_output)
45 	    close(output);
46 	 output=-1;
47 	 need_close_output=false;
48       }
49 
50    bool enabled;
51    int level;
52 
53    xstring buf;
54 
55 protected:
56    void SetOutput(int o,bool need_close);
57 
58 public:
59    static Ref<Log> global;
60 
61    bool WillOutput(int l);
62    void DoWrite(const char *str,int len);
DoWrite(const xstring & str)63    void DoWrite(const xstring &str) { DoWrite(str,str.length()); }
64    void Write(int l,const char *str,int len);
Write(int l,const char * str)65    void Write(int l,const char *str) { Write(l,str,xstrlen(str)); }
Write(int l,const xstring & str)66    void Write(int l,const xstring &str) { Write(l,str,str.length()); }
67    void Format(int l,const char *fmt,...) PRINTF_LIKE(3,4);
68    void vFormat(int l,const char *fmt,va_list v);
69 
SetCB(tty_cb_t cb)70    void SetCB(tty_cb_t cb) { tty_cb=cb; }
71 
IsTTY()72    bool IsTTY() { return tty; }
73 
74    Log(const char *name);
75    ~Log();
76 
77    static void Cleanup();
78 
ResPrefix()79    const char *ResPrefix() const { return "log"; }
ResClosure()80    const char *ResClosure() const { return name; }
81    void Reconfig(const char *);
82 };
83 
84 #define debug(a) do { if(Log::global) Log::global->Format a; } while(0)
85 
86 #endif // LOG_H
87