Browse Source

Update logging logic

Brian Aker 13 years ago
parent
commit
d16b0492d2
4 changed files with 77 additions and 80 deletions
  1. 72 73
      gearmand/gearmand.cc
  2. 2 4
      libgearman-server/gearmand.cc
  3. 2 2
      libgearman-server/log.cc
  4. 1 1
      util/daemon.cc

+ 72 - 73
gearmand/gearmand.cc

@@ -1,5 +1,7 @@
-/* Gearman server and library
- * Copyright (C) 2008 Brian Aker, Eric Day
+/* Gearman server and library 
+ *
+ * Copyright (C) 2011 Data Differential LLC 
+ * Copyright (C) 2008 Brian Aker, Eric Day 
  * All rights reserved.
  *
  * Use and distribution licensed under the BSD license.  See
@@ -80,34 +82,43 @@ inline void message(const std::string &arg, gearmand_error_t rc)
 
 } // namespace error
 
-struct gearmand_log_syslog_st
-{
-public:
-
-  gearmand_log_syslog_st()
-  {
-    openlog("gearmand", LOG_PID | LOG_NDELAY, LOG_USER);
-  }
-
-  ~gearmand_log_syslog_st()
-  {
-    closelog();
-  }
-
-private:
-};
-
 struct gearmand_log_info_st
 {
   std::string filename;
   int fd;
-  time_t reopen;
+  bool opt_syslog;
 
-  gearmand_log_info_st(const std::string &filename_arg) :
+  gearmand_log_info_st(const std::string &filename_arg, bool syslog_arg) :
     filename(filename_arg),
     fd(-1),
-    reopen(0)
+    opt_syslog(syslog_arg)
   {
+    if (opt_syslog)
+    {
+      openlog("gearmand", LOG_PID | LOG_NDELAY, LOG_USER);
+    }
+
+    init();
+  }
+
+  void init()
+  {
+    if (filename.empty())
+    {
+      return;
+    }
+
+    fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
+    if (fd == -1)
+    {
+      if (opt_syslog)
+      {
+        syslog(LOG_ERR, "Could not open log file for writing:%.*s", int(filename.size()), filename.c_str());
+      }
+      error::perror("Could not open log file for writing.");
+
+      fd= STDERR_FILENO;
+    }
   }
 
   int file() const
@@ -115,12 +126,42 @@ struct gearmand_log_info_st
     return fd;
   }
 
+  bool has_file() const
+  {
+    return fd != STDERR_FILENO;
+  }
+
+  void write(gearmand_verbose_t verbose, const char *mesg)
+  {
+    if (has_file())
+    {
+      char buffer[GEARMAN_MAX_ERROR_SIZE];
+      int buffer_length= snprintf(buffer, GEARMAN_MAX_ERROR_SIZE, "%7s %s\n", gearmand_verbose_name(verbose), mesg);
+      if (::write(file(), buffer, buffer_length) == -1)
+      {
+        error::perror("Could not write to log file.");
+        syslog(LOG_EMERG, "gearmand could not open log file %s, got error %s", filename.c_str(), strerror(errno));
+      }
+
+    }
+
+    if (opt_syslog)
+    {
+      syslog(int(verbose), "%7s %s", gearmand_verbose_name(verbose), mesg);
+    }
+  }
+
   ~gearmand_log_info_st()
   {
-    if (fd != -1)
+    if (fd != -1 and fd != STDERR_FILENO)
     {
       close(fd);
     }
+
+    if (opt_syslog)
+    {
+      closelog();
+    }
   }
 };
 
@@ -134,8 +175,6 @@ static bool _set_signals(void);
 static void _shutdown_handler(int signal_arg);
 static void _log(const char *line, gearmand_verbose_t verbose, void *context);
 
-static bool opt_syslog;
-
 int main(int argc, char *argv[])
 {
   int backlog;
@@ -158,6 +197,8 @@ int main(int argc, char *argv[])
   bool opt_round_robin;
   bool opt_daemon;
   bool opt_check_args;
+  bool opt_syslog;
+
 
   boost::program_options::options_description general("General options");
 
@@ -292,8 +333,7 @@ int main(int argc, char *argv[])
     return EXIT_FAILURE;
   }
 
-  gearmand_log_syslog_st log_syslog;
-  gearmand_log_info_st log_info(log_file);
+  gearmand_log_info_st log_info(log_file, opt_syslog);
 
   gearmand_st *_gearmand= gearmand_create(host.empty() ? NULL : host.c_str(),
                                           port.c_str(), threads, backlog,
@@ -319,7 +359,7 @@ int main(int argc, char *argv[])
     }
   }
 
-  if (not protocol.compare("http"))
+  if (protocol.compare("http") == 0)
   {
     if (http.start(_gearmand) != GEARMAN_SUCCESS)
     {
@@ -329,7 +369,7 @@ int main(int argc, char *argv[])
       return EXIT_FAILURE;
     }
   }
-  else if (not protocol.empty())
+  else if (protocol.empty() == 0)
   {
     error::message("Unknown protocol module", protocol.c_str());
     gearmand_free(_gearmand);
@@ -339,8 +379,7 @@ int main(int argc, char *argv[])
 
   if (opt_daemon)
   {
-    bool close_io= int(verbose) == 0 or log_file.size();
-    if (not util::daemon_is_ready(close_io))
+    if (util::daemon_is_ready(true) == false)
     {
       return EXIT_FAILURE;
     }
@@ -456,49 +495,9 @@ static void _shutdown_handler(int signal_arg)
   }
 }
 
-static void _log(const char *line, gearmand_verbose_t verbose, void *context)
+static void _log(const char *mesg, gearmand_verbose_t verbose, void *context)
 {
   gearmand_log_info_st *log_info= static_cast<gearmand_log_info_st *>(context);
-  int fd;
-
-  if (log_info->filename.empty())
-  {
-    fd= 1;
-  }
-  else
-  {
-    time_t t= time(NULL);
 
-    if (log_info->fd != -1 && log_info->reopen < t)
-    {
-      (void) close(log_info->fd);
-      log_info->fd= -1;
-    }
-
-    if (log_info->fd == -1)
-    {
-      log_info->fd= open(log_info->filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
-      if (log_info->fd == -1)
-      {
-	error::perror("Could not open log file for writing.");
-        return;
-      }
-
-      log_info->reopen= t + GEARMAND_LOG_REOPEN_TIME;
-    }
-
-    fd= log_info->fd;
-  }
-
-  char buffer[GEARMAN_MAX_ERROR_SIZE];
-  int buffer_length= snprintf(buffer, GEARMAN_MAX_ERROR_SIZE, "%7s %s\n", gearmand_verbose_name(verbose), line);
-  if (opt_syslog)
-  {
-    syslog(int(verbose), "%.*s", buffer_length, buffer);
-  }
-
-  if (write(log_info->file(), buffer, strlen(buffer)) == -1)
-  {
-    error::perror("Could not write to log file.");
-  }
+  log_info->write(verbose, mesg);
 }

+ 2 - 4
libgearman-server/gearmand.cc

@@ -166,9 +166,6 @@ gearmand_st *gearmand_create(const char *host_arg,
   _global_gearmand= gearmand;
 
   gearmand_set_log_fn(gearmand, log_function, log_context, verbose_arg);
-  char buffer[1024];
-  snprintf(buffer, sizeof(buffer), "Starting up with verbose set to %s", gearmand_verbose_name(verbose_arg));
-  gearmand_info("");
 
   return gearmand;
 }
@@ -276,7 +273,8 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
   /* Initialize server components. */
   if (gearmand->base == NULL)
   {
-    gearmand_info("Starting up");
+    gearmand_log_info(GEARMAN_DEFAULT_LOG_PARAM, "Starting up, verbose set to %s", 
+                      gearmand_verbose_name(gearmand->verbose));
 
     if (gearmand->threads > 0)
     {

+ 2 - 2
libgearman-server/log.cc

@@ -61,7 +61,7 @@ void gearmand_initialize_thread_logging(const char *identity)
 
 static void gearmand_log(const char *position, const char * /* func */, gearmand_verbose_t verbose, const char *format, va_list args)
 {
-  if (not Gearmand())
+  if (Gearmand() == NULL)
   {
     fprintf(stderr, "%s %7s: ", position,  gearmand_verbose_name(verbose));
     vprintf(format, args);
@@ -237,7 +237,7 @@ gearmand_error_t gearmand_log_gerror(const char *position, const char *function,
   {
     va_list args;
 
-    if (not Gearmand() or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
+    if (Gearmand() == NULL or Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR)
     {
       va_start(args, format);
       gearmand_log(position, function, GEARMAND_VERBOSE_ERROR, format, args);

+ 1 - 1
util/daemon.cc

@@ -77,7 +77,7 @@ bool daemon_is_ready(bool close_io)
     return false;
   }
 
-  if (not close_io)
+  if (close_io == false)
   {
     return true;;
   }