## zevents.awk -- analyse Zope logs
##
## Parse Zope-Events logfile-data and extract fields. Output 
## as CSV or SQL format.
##
## (c) 2006 Uli Fouquet
##
## Syntax: awk [-v SQL=0|1] [-v TRACEBACK=0|1] -f zevents.awk <input-file>
##
## Options:
##
## -v SQL if SQL is set, SQL output will be generated.
##              Othwewise you will get CSV output (default:0)
##
## -v TRACEBACK if this option is set to '0', tracebacks will
##              not be included in output (default: 1) 
##
## This Script is GPL software.
##

function quoted( STRING ) {
  #gsub("\"", "\\\"", STRING);
  gsub("\"", "'", STRING);
  #gsub("'", "\\'", STRING);
  return STRING
}
function quoteds( STRING ) {
  #gsub("\"", "\\\"", STRING);
  #gsub("\"", "'", STRING);
  gsub("'", "\"", STRING);
  return STRING
}

BEGIN { 
  RS="------\n"; FS="\n";

  if ( TRACEBACK != "0" ) {
    tbout=",\"TRACEBACK\"";
  }

  if ( SQL != "1" ) {
    print "\"ID\",\"DATE\",\"TIME\",\"LOGLEVEL\",\"MSG_SRC\",\"MSG_TEXT\",\"ERR_TYPE\",\"ERR_MSG\"" tbout;
  }
}
{
  rest = $1;
  pos = index( rest, " ");
  timestamp = substr(rest, 0, pos-1);
  sub("T", " ", timestamp );

  datestr = "";
  timestr = "";

  if ( length( timestamp ) != 0 ) {
    if ( split( timestamp, tsarray, " " ) == 2 )
      {
	datestr = tsarray[1];
	timestr = tsarray[2];
      }
  }

  if ( datestr == "" ) {
    next;
  }


## Create a non-unique ID. DBs may create a primary key of date/time
## and ID.
  myid=sprintf("%07d", NR );

## loglevel...
  rest = substr( rest, pos+1 );
  pos = index( rest, " ");
  loglevel =  substr(rest, 0, pos-1);

## exceptsrc...
  rest = substr( rest, pos+1 );
  pos = index( rest, " ");
  exceptsrc =  substr(rest, 0, pos-1);

## msg...
  msg = substr( rest, pos+1 );

## traceback...
  traceback = "";
  for ( i = 2; i <= NF; i++ ) {
    traceback = traceback "\n" $i;
  }
  traceback = substr( traceback, 2 );


## err_type, err_msg...
  err_type = "";
  err_msg  = "";
  if ( loglevel != "INFO" ) {
    if ( NF > 2 && index( $(NF-1), ":" ) != 0 ) {
      pos = index( $(NF-1), ":" );
      err_type = substr($(NF-1), 0, pos-1);
      err_msg  = substr($(NF-1), pos+1);
    }
  }

  if ( err_type == "" && loglevel == "ERROR" && index( traceback, "notFoundError" ) != 0 ) {
    err_type = "notFoundError";
    err_msg = msg;
  }

  if ( err_type == "" && exceptsrc == "ZPublisher.Conflict" ) {
    err_type = "ConflictError";
    err_msg = ""; #msg;
  }

## output...
  if ( SQL == "1" ) {
    ## Output SQL...

    if ( TRACEBACK != "0" ) {
      output = sprintf( "INSERT INTO \"ZEVENTS\" (\"ID\",\"DATESTR\",\"TIMESTR\",\"LOGLEVEL\",\"MSG_SRC\",\"MSG\",\"ERR_TYPE\",\"ERR_MSG\",\"TRACEBACK\") VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s');", 
			quoteds(myid),
			quoteds(datestr), quoteds(timestr), 
			quoteds(loglevel), quoteds(exceptsrc),
			quoteds(msg), quoteds(err_type), 
			quoteds(err_msg), quoteds(traceback) );
    } else {
      output = sprintf( "INSERT INTO \"ZEVENTS\" (\"ID\",\"DATESTR\",\"TIMESTR\",\"LOGLEVEL\",\"MSG_SRC\",\"MSG\",\"ERR_TYPE\",\"ERR_MSG\") VALUES ('%s','%s','%s','%s','%s','%s','%s','%s');", 
			quoteds(myid),
			quoteds(datestr), quoteds(timestr), 
			quoteds(loglevel), quoteds(exceptsrc),
			quoteds(msg), quoteds(err_type), 
			quoteds(err_msg) );
    }
  } else {
    output = sprintf( "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"", 
		      quoted(myid),
		      quoted(datestr), quoted(timestr), 
		      quoted(loglevel), quoted(exceptsrc),
		      quoted(msg), quoted(err_type), quoted(err_msg) );
    if ( TRACEBACK != "0" ) {
      output = sprintf( "%s,\"%s\"" , output, quoted( traceback ));
    }
  }
  print output;
}
