The '-' character is part of the service name.
[gnucomo.git] / src / gcm_input / syslog_cooker.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 **      FILE NAME      : syslog_cooker.cpp
10 **      SYSTEM NAME    : 
11 **      VERSION NUMBER : $Revision: 1.3 $
12 **
13 **  DESCRIPTION      :  Cooks system log lines
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
18 ***************************************************************************
19 **  ADMINISTRATIVE INFORMATION *
20 ********************************
21 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 **      CREATION DATE   : Aug 06, 2003
23 **      LAST UPDATE     : Aug 06, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: syslog_cooker.cpp,v $
29    Revision 1.3  2007-01-11 13:51:40  arjen
30    The '-' character is part of the service name.
31
32    Revision 1.2  2004/01/13 12:20:12  arjen
33    Accept '/' and '.' characters within the service name
34
35    Revision 1.1  2003/08/11 16:56:16  arjen
36    Different kinds of log files are parsed by a collection of objects
37    of different classes, derived from the base class line_cooker
38    Depending on the message content or the message_type element in
39    XML, one of these objects is selected.
40
41    Logrunner is integrated with gcm_input. Although its functionality
42    is still limited, a connection between logrunner and gcm_input
43    is beginning to form.
44
45 *****************************/
46
47 /* static const char *RCSID = "$Id: syslog_cooker.cpp,v 1.3 2007-01-11 13:51:40 arjen Exp $"; */
48
49 #include <ctype.h>
50
51 #include "syslog_cooker.h"
52
53 static const String syslog_date_re("[[:alpha:]]{3} [ 123][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2}");
54 static const regex re_syslog(syslog_date_re + " [[:alnum:]]+ [[:alpha:]/]+.*:.+");
55
56 bool syslog_cooker::check_pattern(String logline)
57 {
58    return logline == re_syslog;
59 }
60
61 bool syslog_cooker::cook_this(String logline, UTC arrival)
62 {
63    if (check_pattern(logline))
64    {
65       String datestring = logline(0,16);
66       date   log_date = datestring;
67       hour   log_time = datestring;
68       String rest;   //  Rest of the line to be parsed
69       int    i;
70
71       if (log_date.Year() < 0 || log_date.Year() > 2500)
72       {
73          //  The year is not in the log file. Assume the year of arrival,
74          //  unless this puts the log entry at a later date than the arrival date.
75          //  This happens e.g. when a log entry from December arrives in Januari.
76
77          log_date = date(log_date.Day(), log_date.Month(), date(arrival).Year());
78          if (log_date > date(arrival))
79          {
80             log_date = date(log_date.Day(), log_date.Month(), date(arrival).Year() - 1);
81          }
82       }
83
84       ts = UTC(log_date, log_time);
85
86       //  Extract the hostname
87  
88       rest = logline << 16;
89       i = rest.index(' ');
90       hn = rest(0,i);
91
92       //  Extract the service
93
94       rest <<= i + 1;
95       for (i = 0; (isalpha(rest[i])  || rest[i] == '/' || rest[i] == '.' || rest[i] == '-') && i < ~rest; i++);
96       srv = rest(0, i);
97
98       return true;
99    }
100    else
101    {
102       return false;
103    }
104 }
105
106