EMAN2
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
EMAN::Log Class Reference

Log defines a way to output logging information. More...

#include <log.h>

Collaboration diagram for EMAN::Log:
Collaboration graph
[legend]

List of all members.

Public Types

enum  LogLevel { ERROR_LOG, WARNING_LOG, DEBUG_LOG, VARIABLE_LOG }

Public Member Functions

int begin (int argc, char *argv[], int ppid)
 begin() and start() are used by command-line programs
void end (int ref, const string &file="-", const string &text="")
void error (const char *format,...)
 log an error message.
void warn (const char *format,...)
 log a warning message.
void debug (const char *format,...)
 log a debug message.
void variable (const char *format,...)
 log a very-detailed-level debug message.
void set_level (int level)
void set_logfile (const char *filename)
 set log output file.
void loc (LogLevel level, const string &file, int linenum, const string &func)

Static Public Member Functions

static Loglogger ()

Private Member Functions

 Log ()
 Log (const Log &)
 ~Log ()
void vlog (const char *format, LogLevel level, va_list arg)

Private Attributes

FILE * out
LogLevel log_level
string default_emandir
string default_emanlog
string location

Static Private Attributes

static Loginstance = 0

Detailed Description

Log defines a way to output logging information.

1) The logs can either go to standard output (default), or go to a user-given file. 2) 4 verbose log levels are defined. by default, ERROR_LOG is used. 3) Typical usage: Log::logger()->set_level(Log::WARNING_LEVEL); Log::logger()->error("cannot open file");

Definition at line 72 of file log.h.


Member Enumeration Documentation

Enumerator:
ERROR_LOG 
WARNING_LOG 
DEBUG_LOG 
VARIABLE_LOG 

Definition at line 75 of file log.h.

                {
                        ERROR_LOG,        // error message
                        WARNING_LOG,      // warning message
                        DEBUG_LOG,        // debug message, usually at function level.
                        VARIABLE_LOG      // very-detailed-level debug message
                };

Constructor & Destructor Documentation

Log::Log ( ) [private]

Definition at line 53 of file log.cpp.

References default_emandir, default_emanlog, ERROR_LOG, location, log_level, and out.

Referenced by logger().

{
        out = 0;
        log_level = ERROR_LOG;
#ifdef WIN32
        char c[2];
        c[0] = getenv("WINDIR")[0];
        c[1] = '\0';
        default_emandir = string(c) + string(":\\.eman");
#else
        default_emandir = string(getenv("HOME")) + "/.eman";
        mkdir(default_emandir.c_str(), 0xffff);
#endif
        default_emanlog = ".emanlog";
        location = "";
}
Log::Log ( const Log ) [private]

Definition at line 70 of file log.cpp.

{
}
Log::~Log ( ) [private]

Definition at line 74 of file log.cpp.

References out.

{
        if (out) {
                fclose(out);
                out = 0;
        }
}

Member Function Documentation

int Log::begin ( int  argc,
char *  argv[],
int  ppid 
)

begin() and start() are used by command-line programs

Definition at line 181 of file log.cpp.

References default_emandir, default_emanlog, in, and EMAN::Util::sbasename().

{
        time_t tm = time(0);
        const char *pwd = getenv("PWD");
#ifdef  _WIN32
        int ref = _getpid();
#else
        int ref = getpid();
#endif

        string filename = Util::sbasename(argv[0]);

        char s[4048];
#ifndef WIN32
        sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid ? ppid : getppid(), filename.c_str());
#else
        sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid, filename.c_str());
#endif
        for (int i = 1; i < argc; i++) {
                sprintf(s + strlen(s), " %s", argv[i]);
        }
        sprintf(s + strlen(s), "\n");

        FILE *eman_file = fopen(default_emanlog.c_str(), "a");
        if (!eman_file) {
                return 0;
        }

        //Util::file_lock_wait(eman_file);
        fprintf(eman_file, "%s", s);
        fclose(eman_file);

        string dirlist = default_emandir + "./dirlist";
        FILE *in = fopen(dirlist.c_str(), "r");
        if (in) {
                char s[1024];
                int f = 0;
                while (fscanf(in, " %1023s", s) == 1) {
                        if (strcmp(s, pwd) == 0) {
                                f = 1;
                                break;
                        }
                }

                fclose(in);
                if (!f) {
                        in = 0;
                }
        }

        if (!in) {
                FILE *dirout = fopen(dirlist.c_str(), "a");
                if (dirout) {
                        fprintf(dirout, "%s\n", pwd);
                        fclose(dirout);
                }
        }

        return ref;
}
void Log::debug ( const char *  format,
  ... 
)

log a debug message.

log level = DEBUG_LOG. Its args are the same as printf().

Definition at line 144 of file log.cpp.

References DEBUG_LOG, and vlog().

{
        va_list arg;
        va_start(arg, format);
        vlog(format, DEBUG_LOG, arg);
        va_end(arg);
}
void Log::end ( int  ref,
const string &  file = "-",
const string &  text = "" 
)

Definition at line 243 of file log.cpp.

References out.

{
        FILE *out = fopen(".emanlog", "a");

        if (out) {
                time_t tm = time(0);
                //Util::file_lock_wait(out);
                fprintf(out, "%d\t%ld\t%s\t%s\n", ref, tm, file.c_str(), text.c_str());
                fclose(out);
        }
}
void Log::error ( const char *  format,
  ... 
)

log an error message.

log level = ERROR_LOG. Its args are the same as printf().

Definition at line 160 of file log.cpp.

References ERROR_LOG, and vlog().

Referenced by EMAN::CCDNormProcessor::process_inplace().

{
        va_list arg;
        va_start(arg, format);
        vlog(format, ERROR_LOG, arg);
        va_end(arg);
}
void Log::loc ( LogLevel  level,
const string &  file,
int  linenum,
const string &  func 
)

Definition at line 92 of file log.cpp.

References EMAN::Util::int2str(), location, log_level, and EMAN::Util::sbasename().

{
        if (log_level < level) {
                return;
        }

        location = Util::sbasename(filename) + ":" + Util::int2str(linenum);
        if (func != "") {
                location +=" " + func + "()";
        }
}
Log * Log::logger ( ) [static]

Definition at line 84 of file log.cpp.

References instance, and Log().

Referenced by EMAN::CCDNormProcessor::process_inplace().

{
        if (!instance) {
                instance = new Log();
        }
        return instance;
}
void Log::set_level ( int  level)

Definition at line 169 of file log.cpp.

References log_level.

Referenced by EMAN::CCDNormProcessor::process_inplace().

{
        log_level = (LogLevel)level;
}
void Log::set_logfile ( const char *  filename)

set log output file.

If this function is not called, output is standart output.

Definition at line 174 of file log.cpp.

References out.

{
        if (filename && !out) {
                out = fopen(filename, "wb");
        }
}
void Log::variable ( const char *  format,
  ... 
)

log a very-detailed-level debug message.

log level = VARIABLE_LOG. Its args are the same as printf().

Definition at line 136 of file log.cpp.

References VARIABLE_LOG, and vlog().

{
        va_list arg;
        va_start(arg, format);
        vlog(format, VARIABLE_LOG, arg);
        va_end(arg);
}
void Log::vlog ( const char *  format,
LogLevel  level,
va_list  arg 
) [private]

Definition at line 104 of file log.cpp.

References ERROR_LOG, key, location, log_level, out, and WARNING_LOG.

Referenced by debug(), error(), variable(), and warn().

{
        if (log_level < level) {
                return;
        }

        const char *key = "";

        switch (level) {
        case WARNING_LOG:
                key = "Warning: ";
                break;
        case ERROR_LOG:
                key = "Error: ";
                break;
        default:
                key = "";
        }

        FILE *file = stdout;
        if (out) {
                file = out;
        }

        fprintf(file, "%s", key);
        vfprintf(file, format, arg);
        if (location != "") {
                fprintf(file, " at %s", location.c_str());
        }
        fprintf(file, "\n");
}
void Log::warn ( const char *  format,
  ... 
)

log a warning message.

log level = WARNING_LOG. Its args are the same as printf().

Definition at line 152 of file log.cpp.

References vlog(), and WARNING_LOG.

{
        va_list arg;
        va_start(arg, format);
        vlog(format, WARNING_LOG, arg);
        va_end(arg);
}

Member Data Documentation

string EMAN::Log::default_emandir [private]

Definition at line 125 of file log.h.

Referenced by begin(), and Log().

string EMAN::Log::default_emanlog [private]

Definition at line 126 of file log.h.

Referenced by begin(), and Log().

Log * Log::instance = 0 [static, private]

Definition at line 122 of file log.h.

Referenced by logger().

string EMAN::Log::location [private]

Definition at line 127 of file log.h.

Referenced by loc(), Log(), and vlog().

Definition at line 124 of file log.h.

Referenced by loc(), Log(), set_level(), and vlog().

FILE* EMAN::Log::out [private]

Definition at line 123 of file log.h.

Referenced by end(), Log(), set_logfile(), vlog(), and ~Log().


The documentation for this class was generated from the following files: