Blame src/log.h

Packit Service a2489d
/*
Packit Service a2489d
 * lftp - file transfer program
Packit Service a2489d
 *
Packit Service a2489d
 * Copyright (c) 1996-2016 by Alexander V. Lukyanov (lav@yars.free.net)
Packit Service a2489d
 *
Packit Service a2489d
 * This program is free software; you can redistribute it and/or modify
Packit Service a2489d
 * it under the terms of the GNU General Public License as published by
Packit Service a2489d
 * the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
 * (at your option) any later version.
Packit Service a2489d
 *
Packit Service a2489d
 * This program is distributed in the hope that it will be useful,
Packit Service a2489d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
 * GNU General Public License for more details.
Packit Service a2489d
 *
Packit Service a2489d
 * You should have received a copy of the GNU General Public License
Packit Service a2489d
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service a2489d
 */
Packit Service a2489d
Packit Service a2489d
#ifndef LOG_H
Packit Service a2489d
#define LOG_H
Packit Service a2489d
Packit Service a2489d
#include <unistd.h>
Packit Service a2489d
#include <stdlib.h>
Packit Service a2489d
#include <stdarg.h>
Packit Service a2489d
#include "Ref.h"
Packit Service a2489d
#include "ResMgr.h"
Packit Service a2489d
Packit Service a2489d
class Log : public ResClient
Packit Service a2489d
{
Packit Service a2489d
   const char *name;
Packit Service a2489d
   int output;
Packit Service a2489d
   bool need_close_output;
Packit Service a2489d
   bool tty;
Packit Service a2489d
   bool show_pid;
Packit Service a2489d
   bool show_time;
Packit Service a2489d
   bool show_context;
Packit Service a2489d
   bool at_line_start;
Packit Service a2489d
   typedef void (*tty_cb_t)();
Packit Service a2489d
   tty_cb_t tty_cb;
Packit Service a2489d
Packit Service a2489d
   void CloseOutput()
Packit Service a2489d
      {
Packit Service a2489d
	 if(need_close_output)
Packit Service a2489d
	    close(output);
Packit Service a2489d
	 output=-1;
Packit Service a2489d
	 need_close_output=false;
Packit Service a2489d
      }
Packit Service a2489d
Packit Service a2489d
   bool enabled;
Packit Service a2489d
   int level;
Packit Service a2489d
Packit Service a2489d
   xstring buf;
Packit Service a2489d
Packit Service a2489d
protected:
Packit Service a2489d
   void SetOutput(int o,bool need_close);
Packit Service a2489d
Packit Service a2489d
public:
Packit Service a2489d
   static Ref<Log> global;
Packit Service a2489d
Packit Service a2489d
   bool WillOutput(int l);
Packit Service a2489d
   void DoWrite(const char *str,int len);
Packit Service a2489d
   void DoWrite(const xstring &str) { DoWrite(str,str.length()); }
Packit Service a2489d
   void Write(int l,const char *str,int len);
Packit Service a2489d
   void Write(int l,const char *str) { Write(l,str,xstrlen(str)); }
Packit Service a2489d
   void Write(int l,const xstring &str) { Write(l,str,str.length()); }
Packit Service a2489d
   void Format(int l,const char *fmt,...) PRINTF_LIKE(3,4);
Packit Service a2489d
   void vFormat(int l,const char *fmt,va_list v);
Packit Service a2489d
Packit Service a2489d
   void SetCB(tty_cb_t cb) { tty_cb=cb; }
Packit Service a2489d
Packit Service a2489d
   bool IsTTY() { return tty; }
Packit Service a2489d
Packit Service a2489d
   Log(const char *name);
Packit Service a2489d
   ~Log();
Packit Service a2489d
Packit Service a2489d
   static void Cleanup();
Packit Service a2489d
Packit Service a2489d
   const char *ResPrefix() const { return "log"; }
Packit Service a2489d
   const char *ResClosure() const { return name; }
Packit Service a2489d
   void Reconfig(const char *);
Packit Service a2489d
};
Packit Service a2489d
Packit Service a2489d
#define debug(a) do { if(Log::global) Log::global->Format a; } while(0)
Packit Service a2489d
Packit Service a2489d
#endif // LOG_H