• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 32064673974

17 Aug 2026 08:13PM UTC coverage: 81.333% (-0.09%) from 81.419%
32064673974

push

github

web-flow
Replace exported C API error globals (#4065)

18557 of 27007 branches covered (68.71%)

Branch coverage included in aggregate %.

34 of 59 new or added lines in 9 files covered. (57.63%)

60317 of 69970 relevant lines covered (86.2%)

49992887.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/src/error.cpp
1
#include "openmc/error.h"
2

3
#include "openmc/message_passing.h"
4
#include "openmc/settings.h"
5

6
#if defined(__unix__) || defined(__unix) ||                                    \
7
  (defined(__APPLE__) && defined(__MACH__))
8
#include <unistd.h> // for isatty
9
#endif
10

11
#include <cstdlib> // for exit
12
#include <iomanip> // for setw
13
#include <iostream>
14

15
//==============================================================================
16
// Functions
17
//==============================================================================
18

19
namespace openmc {
20

21
namespace {
22

23
std::string error_message;
24

25
} // namespace
26

27
void set_errmsg(const char* message)
82✔
28
{
29
  error_message = message;
82✔
30
}
82✔
31

32
void set_errmsg(const std::string& message)
143✔
33
{
34
  error_message = message;
143✔
35
}
143✔
36

NEW
37
void set_errmsg(const std::stringstream& message)
×
38
{
NEW
39
  error_message = message.str();
×
NEW
40
}
×
41

42
const char* get_errmsg()
1,382,738✔
43
{
44
  return error_message.c_str();
1,382,738✔
45
}
46

47
extern "C" const char* openmc_get_err_msg()
1,382,738✔
48
{
49
  return get_errmsg();
1,382,738✔
50
}
51

52
#ifdef OPENMC_MPI
53
void abort_mpi(int code)
54
{
55
  MPI_Abort(mpi::intracomm, code);
56
}
57
#endif
58

59
void output(const std::string& message, std::ostream& out, int indent)
222,693✔
60
{
61
  // Set line wrapping and indentation
62
  int line_wrap = 80;
222,693✔
63

64
  // Determine length of message
65
  int length = message.size();
222,693✔
66

67
  int i_start = 0;
222,693✔
68
  int line_len = line_wrap - indent + 1;
222,693✔
69
  while (i_start < length) {
233,182!
70
    if (length - i_start < line_len) {
233,182✔
71
      // Remainder of message will fit on line
72
      out << message.substr(i_start) << std::endl;
222,693✔
73
      break;
222,693✔
74

75
    } else {
76
      // Determine last space in current line
77
      std::string s = message.substr(i_start, line_len);
10,489✔
78
      auto pos = s.find_last_of(' ');
10,489✔
79

80
      // Write up to last space, or whole line if no space is present
81
      out << s.substr(0, pos) << '\n' << std::setw(indent) << " ";
20,978✔
82

83
      // Advance starting position
84
      i_start += (pos == std::string::npos) ? line_len : pos + 1;
10,489✔
85
    }
10,489✔
86
  }
87
}
222,693✔
88

89
void warning(const std::string& message)
9,373✔
90
{
91
#ifdef _POSIX_VERSION
92
  // Make output yellow if user is in a terminal
93
  if (isatty(STDERR_FILENO)) {
9,373!
94
    std::cerr << "\033[0;33m";
×
95
  }
96
#endif
97

98
  // Write warning
99
  std::cerr << " WARNING: ";
9,373✔
100
  output(message, std::cerr, 10);
9,373✔
101

102
#ifdef _POSIX_VERSION
103
  // Reset color for terminal
104
  if (isatty(STDERR_FILENO)) {
9,373!
105
    std::cerr << "\033[0m";
×
106
  }
107
#endif
108
}
9,373✔
109

110
void write_message(const std::string& message, int level)
233,968✔
111
{
112
  // Only allow master to print to screen
113
  if (!mpi::master)
233,968✔
114
    return;
115

116
  if (level <= settings::verbosity) {
216,087✔
117
    std::cout << " ";
213,154✔
118
    output(message, std::cout, 1);
213,154✔
119
  }
120
}
121

122
void fatal_error(const std::string& message, int err)
166✔
123
{
124
#pragma omp critical(FatalError)
154✔
125
  {
166✔
126
#ifdef _POSIX_VERSION
127
    // Make output red if user is in a terminal
128
    if (isatty(STDERR_FILENO)) {
166!
129
      std::cerr << "\033[0;31m";
×
130
    }
131
#endif
132

133
    // Write error message
134
    std::cerr << " ERROR: ";
166✔
135
    output(message, std::cerr, 8);
166✔
136

137
#ifdef _POSIX_VERSION
138
    // Reset color for terminal
139
    if (isatty(STDERR_FILENO)) {
166!
140
      std::cerr << "\033[0m";
×
141
    }
142
#endif
143
  }
144

145
#ifdef OPENMC_MPI
146
  MPI_Abort(mpi::intracomm, err);
57✔
147
#endif
148

149
  // Abort the program
150
  std::exit(err);
109✔
151
}
152

153
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc