X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Flogger.c;h=6765cc5abeba7994b1283734714577eeb44f3bf1;hp=8e73a6d3759376eccc2ff0301ae99152c8b8f673;hb=b403f77dd84b7cae86bdaaf4961af515cf7a3fb9;hpb=de78d79db84c486afcc353884ec1770866beb653 diff --git a/src/logger.c b/src/logger.c index 8e73a6d3..6765cc5a 100644 --- a/src/logger.c +++ b/src/logger.c @@ -13,11 +13,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "system.h" @@ -46,14 +44,18 @@ void openlogger(const char *ident, logmode_t mode) { case LOGMODE_FILE: logpid = getpid(); logfile = fopen(logfilename, "a"); - if(!logfile) + if(!logfile) { + fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno)); logmode = LOGMODE_NULL; + } break; case LOGMODE_SYSLOG: #ifdef HAVE_MINGW loghandle = RegisterEventSource(NULL, logident); - if(!loghandle) + if(!loghandle) { + fprintf(stderr, "Could not open log handle!"); logmode = LOGMODE_NULL; + } break; #else #ifdef HAVE_SYSLOG_H @@ -66,8 +68,24 @@ void openlogger(const char *ident, logmode_t mode) { } } +void reopenlogger() { + if(logmode != LOGMODE_FILE) + return; + + fflush(logfile); + FILE *newfile = fopen(logfilename, "a"); + if(!newfile) { + logger(LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno)); + return; + } + fclose(logfile); + logfile = newfile; +} + void logger(int priority, const char *format, ...) { va_list ap; + char timestr[32] = ""; + time_t now; va_start(ap, format); @@ -78,7 +96,9 @@ void logger(int priority, const char *format, ...) { fflush(stderr); break; case LOGMODE_FILE: - fprintf(logfile, "%ld %s[%ld]: ", time(NULL), logident, (long)logpid); + now = time(NULL); + strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now)); + fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid); vfprintf(logfile, format, ap); fprintf(logfile, "\n"); fflush(logfile); @@ -87,7 +107,7 @@ void logger(int priority, const char *format, ...) { #ifdef HAVE_MINGW { char message[4096]; - char *messages[] = {message}; + const char *messages[] = {message}; vsnprintf(message, sizeof(message), format, ap); ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL); }