/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    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
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* midnightcode-syslog.c :: Midnight Code syslog library	*/

/* Includes software to support RFCs :: 3164 */


#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <netdb.h>
#include <ctype.h>
#include <arpa/inet.h>

#include "preferences.h"
#include "midnightcode-list.h"
#include "midnightcode-syslog.h"



char * midnightcode_syslog_facility_l[32] = {
  "kernel messages",
  "user-level messages",
  "mail system",
  "system daemons",
  "security/authorization messages (note 1)",
  "messages generated internally by syslogd",
  "line printer subsystem",
  "network news subsystem",
  "UUCP subsystem",
  "clock daemon (note 2)",
  "security/authorization messages (note 1)",
  "FTP daemon",
  "NTP subsystem",
  "log audit (note 1)",
  "log alert (note 1)",
  "clock daemon (note 2)",
  "local use 0 (local0)",
  "local use 1 (local1)",
  "local use 2 (local2)",
  "local use 3 (local3)",
  "local use 4 (local4)",
  "local use 5 (local5)",
  "local use 6 (local6)",
  "local use 7 (local7)"
};

char * midnightcode_syslog_severity_l[32] = {
  "Emergency: system is unusable", 
  "Alert: action must be taken immediately", 
  "Critical: critical conditions", 
  "Error: error conditions", 
  "Warning: warning conditions", 
  "Notice: normal but significant condition", 
  "Informational: informational messages", 
  "Debug: debug-level messages"
};



int
midnightcode_syslog_facilitycheck(int facility) {
  int ret_i;


  ret_i = 0;
  if(facility > 23 || facility < 0)
    ret_i = 1;

  return ret_i;
}



int
midnightcode_syslog_severitycheck(int severity) {
  int ret_i;


  ret_i = 0;
  if(severity > 7 || severity < 0)
    ret_i = 1;

  return ret_i;
}



int
midnightcode_syslog_priority(int facility, int severity) {
  int ret_i;


  ret_i = -1;
  if(midnightcode_syslog_facilitycheck(facility))
    return ret_i;
  if(midnightcode_syslog_severitycheck(severity))
    return ret_i;

  ret_i = (facility * 8) + severity;

  return ret_i;
}



int
midnightcode_syslog_log(int facility, int severity, char * function,
                       const char *fmt, ...) {

  va_list args;
  char buffer[81];
  int priority;
  int format;


  memset(buffer, 0, sizeof(buffer));

  va_start(args, fmt);
  vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
  va_end(args);

  if((priority = midnightcode_syslog_priority(facility, severity)) < 0)
    return -1;

  format = 0;
  if(function) {
    if(strlen(function))
      format = 1;
  }

  if(format) {
    syslog(priority, "[%s] %s", function, buffer);
  } else {
    syslog(priority, "%s", buffer);
  }


  return 0;
}


