File: output.cc

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,568 kB
  • sloc: cpp: 94,763; ansic: 17,785; sh: 10,701; makefile: 1,403; yacc: 127; lex: 116; awk: 23
file content (206 lines) | stat: -rw-r--r-- 5,802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file output.cc

    @brief Functionality for writing output messages to a text file.

    @author: Elias Rudberg <em>responsible</em>
*/

#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "output.h"
#include "utilities.h"

static int global_memory_usage_output_flag = 0; /* mem output disabled by default */

/* We choose to have output disabled by default, until enable_output() is called, to make sure output is disabled on worker processes when CHT is used. */ 
static int global_output_enabled_flag = 0; /* output disabled by default */

static int use_printf = 0; /* use printf */

/* general output routine  */
void 
do_output(int logCategory, int logArea, const char* format, ...)
{
  va_list a;
  va_start(a, format);
  if(use_printf)
    do_voutput_printf(logCategory, logArea, format, a);
  else
    do_voutput(logCategory, logArea, format, a);
  va_end(a);
}

/* output in stdout using printf */
void 
enable_printf_output()
{
  global_output_enabled_flag = 1;
  use_printf = 1;

  //global_memory_usage_output_flag = 1;
}


int
do_voutput_printf(int logCategory, int logArea, const char* format, va_list a)
{
  if(global_output_enabled_flag == 0)
    return 0; // Output is disabled; do nothing, just return.
  
  char ss[8888]; /* FIXME: Do something nicer here. */
  int r;

  memset(ss, 0, sizeof(ss));
  r = vsnprintf(ss, sizeof(ss), format, a);

  printf("%s\n", ss);

  return r;
}


int
do_voutput(int logCategory, int logArea, const char* format, va_list a)
{
  if(global_output_enabled_flag == 0)
    return 0; // Output is disabled; do nothing, just return.
  
  char ss[8888]; /* FIXME: Do something nicer here. */
  int r;

  memset(ss, 0, sizeof(ss));
  
  /* The output line should begin with two characters 
     specifying the log category, followed by two characters
     specifying the log area. */
  switch(logCategory)
    {
    case LOG_CAT_UNDEFINED: strcat(ss, "  "); break;
    case LOG_CAT_ERROR:     strcat(ss, "ER"); break;
    case LOG_CAT_WARNING:   strcat(ss, "WA"); break;
    case LOG_CAT_INFO:      strcat(ss, "IN"); break;
    case LOG_CAT_EXTRAINFO: strcat(ss, "EX"); break;
    case LOG_CAT_RESULTS:   strcat(ss, "RE"); break;
    case LOG_CAT_TIMINGS:   strcat(ss, "TI"); break;
    case LOG_CAT_MEMUSAGE:  strcat(ss, "ME"); break;
    default:                strcat(ss, "  "); break;
    }
  switch(logArea)
    {
    case LOG_AREA_UNDEFINED: strcat(ss, "  "); break;
    case LOG_AREA_MAIN:      strcat(ss, "MA"); break;
    case LOG_AREA_SCF:       strcat(ss, "SC"); break;
    case LOG_AREA_LR:        strcat(ss, "LR"); break;
    case LOG_AREA_INTEGRALS: strcat(ss, "IN"); break;
    case LOG_AREA_DENSFROMF: strcat(ss, "DE"); break;
    case LOG_AREA_DFT      : strcat(ss, "DF"); break;
    case LOG_AREA_LOWLEVEL:  strcat(ss, "LO"); break;
    case LOG_AREA_CI:        strcat(ss, "CI"); break;
    case LOG_AREA_ED:        strcat(ss, "ED"); break;
    case LOG_AREA_GS:        strcat(ss, "GS"); break;
    default:                 strcat(ss, "  "); break;
    }

  strcat(ss, " ");

  r = vsnprintf(ss+5, sizeof(ss)-5, format, a);

#if 0
  /* This needs to be protected with mutex. */
  if(global_output_file == NULL)
    global_output_file = fopen("ergoscf.out", "wt");
  fprintf(global_output_file, "%s\n", ss);
  fflush(global_output_file);
#else
  /* append to output file */
  {
    FILE *output_file = fopen("ergoscf.out", "at");
    fprintf(output_file, "%s\n", ss);
    fclose(output_file);
  }
#endif
  return r;
}



void
do_output_time(int logCategory, int logArea, const char* s)
{
  int len;
  char timeString[88];
  char ss[222];
  time_t rawtime;
  time(&rawtime);
  strcpy(timeString, ctime(&rawtime));
  len = (int)strlen(timeString);
  if(timeString[len-1] == '\n')
    timeString[len-1] = '\0';
  sprintf(ss, "%s %s", s, timeString);
  do_output(logCategory, logArea, ss);
}

void
enable_memory_usage_output()
{
  global_memory_usage_output_flag = 1;
}

void 
enable_output()
{
  global_output_enabled_flag = 1;
}

void output_current_memory_usage(int logArea, const char* contextString)
{
  double virt, res, virtPeak;

  if(global_memory_usage_output_flag == 0)
    return;

  if(get_memory_usage_by_procfile(&virt, &res, &virtPeak) != 0)
    {
      /* error getting memory usage */
      do_output(LOG_CAT_MEMUSAGE, logArea, "memory usage at '%s': virt ???? res ???? v peak ????",
		contextString);
    }
  else
    {
      do_output(LOG_CAT_MEMUSAGE, logArea, "memory usage at '%s': virt %6.3f G res %6.3f G v peak %6.3f G",
		contextString, virt, res, virtPeak);
    }
}