/* report2.c
   ========= */

/* based on log_error.c v1.1 in control.lib
 * additional log_errorS function
 * which 
 *		by Sessai@NIPR,1998
 */

#include <sys/timers.h>	// no nead??
#include <sys/kernel.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define TIME_OUT 10.0
#define OK 'o'
#define ERROR 'e'

extern pid_t log_pid;
extern char errname[128];

char *get_time2() {
  char *str="YY/MM/DD hh:mm:ss";
  long clock;
  struct tm *gmt;

  time (&clock);
  gmt = gmtime(&clock); 
//  str = asctime(gmt);
//  str[strlen(str)-1] = 0;

	//sprintf(str,"%d/%02d/%02d %02d:%02d:%02d",
	sprintf(str,"%d%02d%02d %02d:%02d:%02d",
		gmt->tm_year,gmt->tm_mon+1,gmt->tm_mday,
		gmt->tm_hour,gmt->tm_min,  gmt->tm_sec);

  return str;
}

/* Sessai additional function
 *	the same as log_error
 *	except that no stderr out by fprintf will be made if no error
 *	mode = 1  is the same as log_error()
 *	mode = 0  is the same as log_error()
 *				except no stderr output if no error
 *
 *							Jan 28, 1998
 */

int log_errorS(char *errlog,char *name,char *buffer,int mode) {

  char tname[32];
  short int size; 
  char msg;
  struct _mxfer_entry send_mx[3];
  struct _mxfer_entry reply_mx[1]; 

  if (buffer == NULL) return -1;
  size=strlen(buffer)+1;

  if (name !=NULL) strncpy(tname,name,32);
  else sprintf(tname,"(unknown)");

  if ((errlog !=NULL) && (strcmp(errname,errlog) !=0)) {
     strcpy(errname,errlog);
     log_pid=0;
  }


  if (log_pid==0) log_pid=qnx_name_locate(0,errname,0,NULL);

//if (log_pid !=0) {
  if (log_pid !=-1) {
    _setmx(reply_mx,&msg,sizeof(msg));
    _setmx(&send_mx[0],tname,sizeof(tname));
    _setmx(&send_mx[1],&size,sizeof(short int));
    _setmx(&send_mx[2],buffer,size);
    Sendmx(log_pid,3,1,send_mx,reply_mx);
  }

  if (msg != OK) fprintf(stderr,"WARNING: Error not logged\n");

//#if 0
//Sessai...
if(mode){
//fprintf(stderr,"%s: %d : %s :%s\n",str_time(),getpid(),tname,buffer);
  fprintf(stderr,"%s:%d:%s:%s\n",get_time2(),getpid(),tname,buffer);
}
//#endif

  if (msg !=OK) return -1;
  return 0;
}