/ logging.c
logging.c
  1  /*
  2   * Copyright 2011-2012 Con Kolivas
  3   * Copyright 2013 Andrew Smith
  4   *
  5   * This program is free software; you can redistribute it and/or modify it
  6   * under the terms of the GNU General Public License as published by the Free
  7   * Software Foundation; either version 3 of the License, or (at your option)
  8   * any later version.  See COPYING for more details.
  9   */
 10  
 11  #include "config.h"
 12  
 13  #include <unistd.h>
 14  
 15  #include "logging.h"
 16  #include "miner.h"
 17  
 18  bool opt_debug = false;
 19  bool opt_log_output = false;
 20  
 21  /* per default priorities higher than LOG_NOTICE are logged */
 22  int opt_log_level = LOG_NOTICE;
 23  
 24  static void my_log_curses(int prio, const char *datetime, const char *str, bool force)
 25  {
 26  	if (opt_quiet && prio != LOG_ERR)
 27  		return;
 28  
 29  	/* Mutex could be locked by dead thread on shutdown so forcelog will
 30  	 * invalidate any console lock status. */
 31  	if (force) {
 32  		mutex_trylock(&console_lock);
 33  		mutex_unlock(&console_lock);
 34  	}
 35  #ifdef HAVE_CURSES
 36  	extern bool use_curses;
 37  	if (use_curses && log_curses_only(prio, datetime, str))
 38  		;
 39  	else
 40  #endif
 41  	{
 42  		mutex_lock(&console_lock);
 43  		printf("%s%s%s", datetime, str, "                    \n");
 44  		mutex_unlock(&console_lock);
 45  	}
 46  }
 47  
 48  /* high-level logging function, based on global opt_log_level */
 49  
 50  /*
 51   * log function
 52   */
 53  void _applog(int prio, const char *str, bool force)
 54  {
 55  #ifdef HAVE_SYSLOG_H
 56  	if (use_syslog) {
 57  		syslog(LOG_LOCAL0 | prio, "%s", str);
 58  	}
 59  #else
 60  	if (0) {}
 61  #endif
 62  	else {
 63  		char datetime[64];
 64  		struct timeval tv = {0, 0};
 65  		struct tm *tm;
 66  
 67  		cgtime(&tv);
 68  
 69  		const time_t tmp_time = tv.tv_sec;
 70  		int ms = (int)(tv.tv_usec / 1000);
 71  		tm = localtime(&tmp_time);
 72  
 73  		snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d.%03d] ",
 74  			tm->tm_year + 1900,
 75  			tm->tm_mon + 1,
 76  			tm->tm_mday,
 77  			tm->tm_hour,
 78  			tm->tm_min,
 79  			tm->tm_sec, ms);
 80  
 81  		/* Only output to stderr if it's not going to the screen as well */
 82  		if (!isatty(fileno((FILE *)stderr))) {
 83  			fprintf(stderr, "%s%s\n", datetime, str);	/* atomic write to stderr */
 84  			fflush(stderr);
 85  		}
 86  
 87  		my_log_curses(prio, datetime, str, force);
 88  	}
 89  }
 90  
 91  void _simplelog(int prio, const char *str, bool force)
 92  {
 93  #ifdef HAVE_SYSLOG_H
 94  	if (use_syslog) {
 95  		syslog(LOG_LOCAL0 | prio, "%s", str);
 96  	}
 97  #else
 98  	if (0) {}
 99  #endif
100  	else {
101  		/* Only output to stderr if it's not going to the screen as well */
102  		if (!isatty(fileno((FILE *)stderr))) {
103  			fprintf(stderr, "%s\n", str);	/* atomic write to stderr */
104  			fflush(stderr);
105  		}
106  
107  		my_log_curses(prio, "", str, force);
108  	}
109  }