diff --git a/common/log.c b/common/log.c index ec5ef8d7..f660d247 100644 --- a/common/log.c +++ b/common/log.c @@ -19,6 +19,7 @@ static int ccnet_log_level; static int seafile_log_level; static char *logfile; static FILE *logfp; +static gboolean to_stdout = FALSE; #ifndef WIN32 #ifdef SEAFILE_SERVER @@ -40,6 +41,8 @@ get_syslog_level (GLogLevelFlags level) return LOG_WARNING; case G_LOG_LEVEL_ERROR: return LOG_ERR; + case G_LOG_LEVEL_CRITICAL: + return LOG_ERR; default: return LOG_DEBUG; } @@ -59,6 +62,10 @@ seafile_log (const gchar *log_domain, GLogLevelFlags log_level, if (log_level > seafile_log_level) return; + if (to_stdout) { + fputs ("[seaf-server] ", logfp); + } + t = time(NULL); tm = localtime(&t); len = strftime (buf, 1024, "[%Y-%m-%d %H:%M:%S] ", tm); @@ -66,11 +73,13 @@ seafile_log (const gchar *log_domain, GLogLevelFlags log_level, if (logfp) { fputs (buf, logfp); if (log_level == G_LOG_LEVEL_DEBUG) - fputs ("[debug] ", logfp); + fputs ("[DEBUG] ", logfp); else if (log_level == G_LOG_LEVEL_WARNING) - fputs ("[warning] ", logfp); + fputs ("[WARNING] ", logfp); + else if (log_level == G_LOG_LEVEL_CRITICAL) + fputs ("[ERROR] ", logfp); else - fputs ("[info] ", logfp); + fputs ("[INFO] ", logfp); fputs (message, logfp); fflush (logfp); } @@ -102,11 +111,13 @@ ccnet_log (const gchar *log_domain, GLogLevelFlags log_level, if (logfp) { fputs (buf, logfp); if (log_level == G_LOG_LEVEL_DEBUG) - fputs ("[debug] ", logfp); + fputs ("[DEBUG] ", logfp); else if (log_level == G_LOG_LEVEL_WARNING) - fputs ("[warning] ", logfp); + fputs ("[WARNING] ", logfp); + else if (log_level == G_LOG_LEVEL_CRITICAL) + fputs ("[ERROR] ", logfp); else - fputs ("[info] ", logfp); + fputs ("[INFO] ", logfp); fputs (message, logfp); fflush (logfp); } @@ -144,9 +155,11 @@ seafile_log_init (const char *_logfile, const char *ccnet_debug_level_str, ccnet_log_level = get_debug_level(ccnet_debug_level_str, G_LOG_LEVEL_INFO); seafile_log_level = get_debug_level(seafile_debug_level_str, G_LOG_LEVEL_DEBUG); - if (strcmp(_logfile, "-") == 0) { + const char *log_to_stdout = g_getenv("SEAFILE_LOG_TO_STDOUT"); + if (g_strcmp0(_logfile, "-") == 0 || g_strcmp0(log_to_stdout, "true") == 0) { logfp = stdout; logfile = g_strdup (_logfile); + to_stdout = TRUE; } else { logfile = ccnet_expand_path(_logfile); diff --git a/common/log.h b/common/log.h index a7d45ace..080ebe15 100644 --- a/common/log.h +++ b/common/log.h @@ -11,6 +11,10 @@ #define seaf_message(fmt, ...) g_message("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__) #endif +#ifndef seaf_error +#define seaf_error(fmt, ...) g_critical("%s(%d): " fmt, __FILE__, __LINE__, ##__VA_ARGS__) +#endif + int seafile_log_init (const char *logfile, const char *ccnet_debug_level_str, const char *seafile_debug_level_str); diff --git a/controller/seafile-controller.c b/controller/seafile-controller.c index a81fce17..cc47049f 100644 --- a/controller/seafile-controller.c +++ b/controller/seafile-controller.c @@ -202,6 +202,7 @@ start_seaf_server () "-l", logfile, "-P", ctl->pidfile[PID_SERVER], "-p", ctl->rpc_pipe_path, + "-f", NULL}; int pid = spawn_process (argv, false); @@ -568,8 +569,8 @@ seaf_controller_init (SeafileController *ctl, char *topdir = g_path_get_dirname(config_dir); logdir = g_build_filename (topdir, "logs", NULL); if (checkdir_with_mkdir(logdir) < 0) { - fprintf (stderr, "failed to create log folder \"%s\": %s\n", - logdir, strerror(errno)); + seaf_error ("failed to create log folder \"%s\": %s\n", + logdir, strerror(errno)); return -1; } g_free (topdir); @@ -683,12 +684,13 @@ set_signal_handlers () static void usage () { - fprintf (stderr, "Usage: seafile-controller OPTIONS\n" - "OPTIONS:\n" - " -b, --bin-dir insert a directory in front of the PATH env\n" - " -c, --config-dir ccnet config dir\n" - " -d, --seafile-dir seafile dir\n" - ); + seafile_log_init ("-", "debug", "debug"); + seaf_error ("Usage: seafile-controller OPTIONS\n" + "OPTIONS:\n" + " -b, --bin-dir insert a directory in front of the PATH env\n" + " -c, --config-dir ccnet config dir\n" + " -d, --seafile-dir seafile dir\n" + ); } /* seafile-controller -t is used to test whether config file is valid */ @@ -717,9 +719,8 @@ test_config (const char *central_config_dir, &error); if (error != NULL) { - fprintf (stderr, - "failed to run \"seaf-server -t\": %s\n", - error->message); + seaf_error ("failed to run \"seaf-server -t\": %s\n", + error->message); exit (1); } @@ -732,8 +733,7 @@ test_config (const char *central_config_dir, } if (retcode != 0) { - fprintf (stderr, - "failed to run \"seaf-server -t\" [%d]\n", retcode); + seaf_error ("failed to run \"seaf-server -t\" [%d]\n", retcode); exit (1); } @@ -869,7 +869,9 @@ int main (int argc, char **argv) exit(1); break; case 'v': - fprintf (stderr, "seafile-controller version 1.0\n"); + seafile_log_init ("-", "debug", "debug"); + seaf_message ("seafile-controller version 1.0\n"); + exit(1); break; case 't': test_conf = TRUE; @@ -911,13 +913,21 @@ int main (int argc, char **argv) g_thread_init (NULL); #endif + char *logfile = g_build_filename (ctl->logdir, "controller.log", NULL); + if (seafile_log_init (logfile, ccnet_debug_level_str, + seafile_debug_level_str) < 0) { + seafile_log_init ("-", ccnet_debug_level_str, seafile_debug_level_str); + seaf_error ("Failed to init log.\n"); + controller_exit (1); + } + if (!seafile_dir) { - fprintf (stderr, " must be specified with --seafile-dir\n"); + seaf_error (" must be specified with --seafile-dir\n"); exit(1); } if (!central_config_dir) { - fprintf (stderr, " must be specified with --central-config-dir\n"); + seaf_error (" must be specified with --central-config-dir\n"); exit(1); } @@ -934,13 +944,6 @@ int main (int argc, char **argv) controller_exit(1); } - char *logfile = g_build_filename (ctl->logdir, "controller.log", NULL); - if (seafile_log_init (logfile, ccnet_debug_level_str, - seafile_debug_level_str) < 0) { - seaf_warning ("Failed to init log.\n"); - controller_exit (1); - } - if (init_syslog_config () < 0) { controller_exit (1); } diff --git a/fileserver/fileserver.go b/fileserver/fileserver.go index f868eaee..aa335674 100644 --- a/fileserver/fileserver.go +++ b/fileserver/fileserver.go @@ -44,6 +44,8 @@ var logFp *os.File var dbType string var seafileDB, ccnetDB *sql.DB +var toStdout bool + func init() { flag.StringVar(¢ralDir, "F", "", "central config directory") flag.StringVar(&dataDir, "d", "", "seafile data directory") @@ -51,6 +53,11 @@ func init() { flag.StringVar(&rpcPipePath, "p", "", "rpc pipe path") flag.StringVar(&pidFilePath, "P", "", "pid file path") + env := os.Getenv("SEAFILE_LOG_TO_STDOUT") + if env == "true" { + toStdout = true + } + log.SetFormatter(&LogFormatter{}) } @@ -61,8 +68,21 @@ const ( type LogFormatter struct{} func (f *LogFormatter) Format(entry *log.Entry) ([]byte, error) { - level := fmt.Sprintf("[%s] ", entry.Level.String()) - buf := make([]byte, 0, len(timestampFormat)+len(level)+len(entry.Message)+1) + levelStr := entry.Level.String() + if levelStr == "fatal" { + levelStr = "ERROR" + } else { + levelStr = strings.ToUpper(levelStr) + } + level := fmt.Sprintf("[%s] ", levelStr) + appName := "" + if toStdout { + appName = "[fileserver] " + } + buf := make([]byte, 0, len(appName)+len(timestampFormat)+len(level)+len(entry.Message)+1) + if toStdout { + buf = append(buf, appName...) + } buf = entry.Time.AppendFormat(buf, timestampFormat) buf = append(buf, level...) buf = append(buf, entry.Message...) diff --git a/fuse/seaf-fuse.c b/fuse/seaf-fuse.c index 60da1abf..f6df60de 100644 --- a/fuse/seaf-fuse.c +++ b/fuse/seaf-fuse.c @@ -320,7 +320,8 @@ int main(int argc, char *argv[]) if (seafile_log_init(logfile, ccnet_debug_level_str, seafile_debug_level_str) < 0) { - fprintf(stderr, "Failed to init log.\n"); + seafile_log_init("-", ccnet_debug_level_str, seafile_debug_level_str); + seaf_error("Failed to init log.\n"); exit(1); } diff --git a/server/gc/seaf-fsck.c b/server/gc/seaf-fsck.c index 171628f6..93f9b413 100644 --- a/server/gc/seaf-fsck.c +++ b/server/gc/seaf-fsck.c @@ -30,9 +30,9 @@ static const struct option long_opts[] = { static void usage () { - fprintf (stderr, - "usage: seaf-fsck [-r] [-E exported_path] [-c config_dir] [-d seafile_dir] " - "[repo_id_1 [repo_id_2 ...]]\n"); + seafile_log_init ("-", "info", "debug"); + seaf_error ("usage: seaf-fsck [-r] [-E exported_path] [-c config_dir] [-d seafile_dir] " + "[repo_id_1 [repo_id_2 ...]]\n"); } #ifdef WIN32 diff --git a/server/gc/seafserv-gc.c b/server/gc/seafserv-gc.c index d816eac5..a621d5cc 100644 --- a/server/gc/seafserv-gc.c +++ b/server/gc/seafserv-gc.c @@ -31,14 +31,14 @@ static const struct option long_opts[] = { static void usage () { - fprintf (stderr, - "usage: seafserv-gc [-c config_dir] [-d seafile_dir] " - "[repo_id_1 [repo_id_2 ...]]\n" - "Additional options:\n" - "-r, --rm-deleted: remove garbaged repos\n" - "-R, --rm-fs: remove fs object\n" - "-D, --dry-run: report blocks that can be remove, but not remove them\n" - "-V, --verbose: verbose output messages\n"); + seafile_log_init ("-", "info", "debug"); + seaf_error ("usage: seafserv-gc [-c config_dir] [-d seafile_dir] " + "[repo_id_1 [repo_id_2 ...]]\n" + "Additional options:\n" + "-r, --rm-deleted: remove garbaged repos\n" + "-R, --rm-fs: remove fs object\n" + "-D, --dry-run: report blocks that can be remove, but not remove them\n" + "-V, --verbose: verbose output messages\n"); } #ifdef WIN32 diff --git a/server/seaf-server.c b/server/seaf-server.c index 6a589cde..245caee2 100644 --- a/server/seaf-server.c +++ b/server/seaf-server.c @@ -51,7 +51,8 @@ static struct option long_options[] = { static void usage () { - fprintf (stderr, "usage: seaf-server [-c config_dir] [-d seafile_dir]\n"); + seafile_log_init ("-", "info", "debug"); + seaf_error ("usage: seaf-server [-c config_dir] [-d seafile_dir]\n"); } #include @@ -1186,10 +1187,7 @@ test_seafile_config(const char *central_config_dir, const char *config_dir, cons central_config_dir = ccnet_expand_path (central_config_dir); } - if (seafile_log_init ("-", "debug", "debug") < 0) { - fprintf (stderr, "seafile_log_init error: %s\n", strerror(errno)); - return -1; - } + seafile_log_init ("-", "debug", "debug"); srand (time(NULL)); @@ -1197,7 +1195,7 @@ test_seafile_config(const char *central_config_dir, const char *config_dir, cons seaf = seafile_session_new (central_config_dir, seafile_dir, config_dir, NULL, NULL); if (!seaf) { - fprintf (stderr, "Error: failed to create ccnet session\n"); + seaf_error ("Error: failed to create ccnet session\n"); return -1; }