Exemple #1
0
static void write_to_log(char *txt, struct s_log *log, int8_t do_flush)
{
	char sbuf[16];

#ifdef CS_ANTICASC
	if (!strncmp(txt + log->header_len, "acasc:", 6)) {
		strcat(txt, "\n");
		switch_log(cfg.ac_logfile, &fpa, ac_init_log);
		if (fpa) {
			fputs(txt + 8, fpa);
			if (do_flush) fflush(fpa);
		}
	} else
#endif
	{
		if (cfg.logtosyslog)
			syslog(LOG_INFO, "%s", txt+24);
		strcat(txt, "\n");
	}
	cs_write_log(txt + 8, do_flush);

#if defined(WEBIF) || defined(MODULE_MONITOR)
	if (loghist && exit_oscam != 1) {
		char *usrtxt = log->cl_text;
		char *target_ptr = NULL;
		int32_t target_len = strlen(usrtxt) + (strlen(txt) - 8) + 1;

		cs_writelock(&loghistory_lock);
		char *lastpos = loghist + (cfg.loghistorysize) - 1;
		if(loghist + target_len + 1 >= lastpos){
			strncpy(txt + 39, "Log entry too long!", strlen(txt) - 39);	// we can assume that the min loghistorysize is always 1024 so we don't need to check if this new string fits into it!
			target_len = strlen(usrtxt) + (strlen(txt) - 8) + 1;
		}
		if (!loghistptr)
			loghistptr = loghist;

		if (loghistptr + target_len + 1 > lastpos) {
			*loghistptr='\0';
			loghistptr=loghist + target_len + 1;
			*loghistptr='\0';
			target_ptr=loghist;
		} else {
			target_ptr = loghistptr;
			loghistptr=loghistptr + target_len + 1;
			*loghistptr='\0';
		}
		cs_writeunlock(&loghistory_lock);

		snprintf(target_ptr, target_len + 1, "%s\t%s", usrtxt, txt + 8);
	}
#endif

	struct s_client *cl;
	for (cl=first_client; cl ; cl=cl->next) {
		if ((cl->typ == 'm') && (cl->monlvl>0) && cl->log) //this variable is only initialized for cl->typ = 'm'
		{
			if (cl->monlvl<2) {
				if (log->cl_typ != 'c' && log->cl_typ != 'm')
					continue;
				if (log->cl_usr && cl->account && strcmp(log->cl_usr, cl->account->usr))
					continue;
			}
			snprintf(sbuf, sizeof(sbuf), "%03d", cl->logcounter);
			cl->logcounter = (cl->logcounter+1) % 1000;
			memcpy(txt + 4, sbuf, 3);
#ifdef MODULE_MONITOR
			monitor_send_idx(cl, txt);
#endif
		}
	}
}
Exemple #2
0
static void write_to_log(char *txt, struct s_log *log, int8_t do_flush)
{
	if(logStarted == 0)
		{ return; }
	
	(void)log; // Prevent warning when WEBIF, MODULE_MONITOR and CS_ANTICASC are disabled

	// anticascading messages go to their own log
	if (!anticasc_logging(txt + log->header_date_offset))
	{
		if(cfg.logtosyslog)
		{
			syslog(LOG_INFO, "%s", txt + log->header_info_offset);
		}
			
		if (cfg.sysloghost != NULL && syslog_socket != -1)
		{	
			char tmp[128+LOG_BUF_SIZE];			
			static char hostname[64];
			static uint8_t have_hostname = 0;
			time_t walltime;
			struct tm lt;
			char timebuf[32];
						
			if(!have_hostname)
			{
				if(gethostname(hostname, 64) != 0)
				{
					cs_strncpy(hostname, "unknown", 64);
				}
				
				have_hostname = 1;
			}
										
			walltime = cs_time();
			localtime_r(&walltime, &lt);

			if(strftime(timebuf, 32, "%b %d %H:%M:%S", &lt) == 0)
			{
				cs_strncpy(timebuf, "unknown", 32);
			}			
			
			snprintf(tmp, sizeof(tmp), "%s %s oscam[%u]: %s", timebuf, hostname, getpid(), txt + log->header_info_offset);
			sendto(syslog_socket, tmp, strlen(tmp), 0, (struct sockaddr*) &syslog_addr, sizeof(syslog_addr));
		}
	}
	
	strcat(txt, "\n");
	cs_write_log(txt, do_flush, log->header_date_offset, log->header_time_offset);

#if defined(WEBIF) || defined(MODULE_MONITOR)
	if(loghist && !exit_oscam && cfg.loghistorysize)
	{
		char *usrtxt = log->cl_text;
		char *target_ptr = NULL;
		int32_t target_len = strlen(usrtxt) + strlen(txt+log->header_date_offset) + 1;

		cs_writelock_nolog(__func__, &loghistory_lock);
		char *lastpos = loghist + (cfg.loghistorysize) - 1;
		if(loghist + target_len + 1 >= lastpos)
		{
			// we can assume that the min loghistorysize is always 1024 so we don't need to check if this new string fits into it!
			strncpy(txt + log->header_len, "Log entry too long!", strlen(txt) - log->header_len);
			target_len = strlen(usrtxt) + strlen(txt+log->header_date_offset) + 1;
		}
		if(!loghistptr)
			{ loghistptr = loghist;	}

		if(loghistptr + target_len + 1 > lastpos)
		{
			*loghistptr = '\0';
			loghistptr = loghist + target_len + 1;
			*loghistptr = '\0';
			target_ptr = loghist;
		}
		else
		{
			target_ptr = loghistptr;
			loghistptr = loghistptr + target_len + 1;
			*loghistptr = '\0';
		}
		++counter;
		
		snprintf(target_ptr, target_len + 1, "%s\t%s", usrtxt, txt + log->header_date_offset);
		ull2b_buf(counter, (uchar *)(loghistid + ((target_ptr-loghist)/3)));
		
		cs_writeunlock_nolog(__func__, &loghistory_lock);
	}
#endif

#if defined(MODULE_MONITOR)
	char sbuf[16];
	struct s_client *cl;
	for(cl = first_client; cl ; cl = cl->next)
	{
		if((cl->typ == 'm') && (cl->monlvl > 0) && cl->log)  //this variable is only initialized for cl->typ = 'm'
		{
			if(cl->monlvl < 2)
			{
				if(log->cl_typ != 'c' && log->cl_typ != 'm')
					{ continue; }
				if(log->cl_usr && cl->account && strcmp(log->cl_usr, cl->account->usr))
					{ continue; }
			}
			
			if(log->header_len > 0)
			{
				snprintf(sbuf, sizeof(sbuf), "%03d", cl->logcounter);
				cl->logcounter = (cl->logcounter + 1) % 1000;
				memcpy(txt + log->header_logcount_offset, sbuf, 3);
				monitor_send_idx(cl, txt);
			}
			else
			{
				char tmp_log[8+LOG_BUF_SIZE];
				snprintf(tmp_log, sizeof(tmp_log), "[LOG%03d]%s", cl->logcounter, txt);
				cl->logcounter = (cl->logcounter + 1) % 1000;
				monitor_send_idx(cl, tmp_log);
			}
		}
	}
#endif
}