- pid_t pid = getpid();
- char buffer[PATH_MAX];
- char procname[16];
- FILE * statfile;
- char *ptr;
-
- sprintf(buffer, "/proc/%d/stat", pid);
- statfile = fopen(buffer, "rt");
- fgets(buffer, PATH_MAX-1, statfile);
- fclose(statfile);
-
- ptr = (char *)strrchr(buffer, ')');
- *ptr = '\0';
- memset(procname, 0, sizeof procname);
- sscanf(buffer, "%d (%15c", &pid, procname); /* comm[16] in kernel */
-
- set_processname(procname);
-}
-
-/* =========================================================================
- Internal function definitions
- ========================================================================= */
-
-static char *get_command_name(char *commandpath)
-{
- char *ptr;
-#ifdef DEBUG_OUTPUT_TO_SCREEN
- printf("getting command name %s\n",commandpath);
-#endif
- ptr = (char *)strrchr(commandpath, '/');
- if (ptr==NULL)
- ptr = commandpath;
- else
- ptr++;
-
-#ifdef DEBUG_OUTPUT_TO_SCREEN
- printf("Concluded %s\n", ptr);
-#endif
- return ptr;
-}
-
-void workout_what_to_print(struct what_to_print_array *what_to_print,
- struct tag_log_file_data *log_struct)
-{
- /* is this a syslog entry? */
- if (log_struct->log_filename[0]==0)
- {
- what_to_print->print_datetime = false;
- what_to_print->print_processname = false;
- what_to_print->print_pid = false;
- }
- else
- {
- what_to_print->print_datetime = true;
- what_to_print->print_processname = true;
-
- /* pid is dealt with at the syslog level if we're syslogging */
- what_to_print->print_pid =
- (((log_struct->display_options & logoption_pid) == 0)?false:true);
- }
-
- what_to_print->print_srcfile =
- (((log_struct->display_options & logoption_nfile) == 0)?true:false);
- what_to_print->print_srcline =
- (((log_struct->display_options & logoption_nline) == 0)?true:false);
-
- what_to_print->print_errlevel = true;
- what_to_print->print_errtype = true;
-}
-
-void generate_message_details(char *message_details_buffer,
- int message_details_buffer_length,
- struct tag_log_file_data *log_struct,
- enum loglevels loglevel, enum logtypes logtype)
-{
-#if 0
- char datebuffer[32];
- char processinfo[64];
- char log_buffer[MAXLOGSIZE];
- const char *logtype_string;
-
- char loglevel_string[12]; /* max int size is 2 billion, or 10 digits */
-#endif
-
- char *ptr = message_details_buffer;
- int templen;
- int len = message_details_buffer_length;
-
-
- struct what_to_print_array what_to_print;
-
- workout_what_to_print(&what_to_print, log_struct);
-
-#ifdef DEBUG_OUTPUT_TO_SCREEN
- printf("Making MessageDetails\n");
-#endif
-
- *ptr = 0;
- /*
- datebuffer[0] = 0;
- ptr = datebuffer;
- */
-
- if (what_to_print.print_datetime)
- {
- time_t thetime;
- time(&thetime);
-
- /* some people might prefer localtime() to gmtime() */
- strftime(ptr, len, "%b %d %H:%M:%S", localtime(&thetime));
-#ifdef DEBUG_OUTPUT_TO_SCREEN
- printf("date is %s\n", ptr);
-#endif
-
- templen = strlen(ptr);
- len -= templen;
- if (what_to_print.print_processname || what_to_print.print_pid)
- strncat(ptr, " ", len);
- else
- strncat(ptr, ":", len);
-
- templen++;
- len --;
- ptr += templen;
- }
-
- /*
- processinfo[0] = 0;
- ptr = processinfo;
- */
-
- if (what_to_print.print_processname)
- {
- strncpy(ptr, global_log_data.processname, len);
-
- templen = strlen(ptr);
- len -= templen;
- ptr += templen;
- }
-
- if (what_to_print.print_pid)
- {
- pid_t pid = getpid();
-
- sprintf(ptr, "[%d]", pid);
-
- templen = strlen(ptr);
- len -= templen;
- ptr += templen;
- }
-
- if (what_to_print.print_srcfile || what_to_print.print_srcline)
- {
- char sprintf_buffer[8];
- char *buff_ptr=NULL;
-
- sprintf_buffer[0] = '[';
- if (what_to_print.print_srcfile)
- {
- strcpy(&sprintf_buffer[1], "%s");
- buff_ptr = &sprintf_buffer[3];
- }
- if (what_to_print.print_srcfile && what_to_print.print_srcline)
- {
- strcpy(&sprintf_buffer[3], ":");
- buff_ptr = &sprintf_buffer[4];
- }
- if (what_to_print.print_srcline)
- {
- strcpy(buff_ptr, "%d");
- buff_ptr = &buff_ptr[2];
- }
- strcpy(buff_ptr, "]");
-
- /*
- ok sprintf string is ready, now is the 1st parameter src or linenumber