Debian Codegraph @

packages/seacas/libraries/suplib_c/add_to_log.c

raw
1 /*
2 * Copyright(C) 1999-2021, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \
10 !defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__)
11 /* Currently we just disable this functionality for windows-based systems... */
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <sys/times.h>
16 #include <sys/utsname.h>
17
18 #include <time.h>
19 #include <unistd.h>
20
21 #ifndef __USE_XOPEN
22 #define __USE_XOPEN
23 #endif
24 #include <stdio.h>
25 #endif
26
27 void add_to_log(const char *my_name, double elapsed)
28 {
29 #if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \
30 !defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__)
31 #define LEN 512
32 /* Don't log information if this environment variable is set */
33 if (getenv("SEACAS_NO_LOGGING") != NULL) {
34 fprintf(stderr, "SEACAS Audit logging disabled via SEACAS_NO_LOGGING setting.\n");
35 return;
36 }
37
38 /* Now try to find the $ACCESS/etc/audit.log file */
39 /* Don't need to try too hard since information is not critical; just useful */
40 char *access_dir = getenv("ACCESS");
41 if (access_dir != NULL) {
42 char filename[LEN];
43 snprintf(filename, LEN, "%s/etc/audit.log", access_dir);
44 FILE *audit = fopen(filename, "a");
45 if (audit != NULL) {
46 const char *codename = strrchr(my_name, '/');
47
48 const char *username = getlogin();
49 if (username == NULL) {
50 username = getenv("LOGNAME");
51 }
52 if (username == NULL) {
53 username = "UNKNOWN";
54 }
55
56 if (codename == NULL) {
57 codename = my_name;
58 }
59 else {
60 codename++;
61 }
62
63 char time_string[LEN];
64 time_t calendar_time = time(NULL);
65 struct tm *local_time = localtime(&calendar_time);
66 strftime(time_string, LEN, "%a %b %d %H:%M:%S %Z %Y", local_time);
67
68 int ticks_per_second;
69 struct tms time_buf;
70 times(&time_buf);
71 ticks_per_second = sysconf(_SC_CLK_TCK);
72 double u_time = (double)(time_buf.tms_utime + time_buf.tms_cutime) / ticks_per_second;
73 double s_time = (double)(time_buf.tms_stime + time_buf.tms_cstime) / ticks_per_second;
74
75 struct utsname sys_info;
76 uname(&sys_info);
77
78 int minutes = (int)(elapsed / 60.0);
79 double seconds = elapsed - minutes * 60.0;
80
81 char log_string[LEN];
82 snprintf(log_string, LEN, "%s %s %s %.3fu %.3fs %d:%5.2f 0.0%% 0+0k 0+0io 0pf+0w %s\n",
83 codename, username, time_string, u_time, s_time, minutes, seconds,
84 sys_info.nodename) < 0
85 ? abort()
86 : (void)0;
87
88 fprintf(audit, "%s", log_string);
89 fclose(audit);
90 }
91 }
92 #else
93 /* Try to avoid unused variable warning/error on windows-based system */
94 (void)my_name;
95 (void)elapsed;
96 #endif
97 }