Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 96 additions & 44 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,115 @@
#include "debug.h"
#endif

#ifdef __GNUG__
#define _print_stack() { \
print_stack(); \
}
#else
#define _print_stack() { }
#endif
#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define RESET "\033[0;0m"

/*
Define utility log functions:
ester_debug|info|warn|err|critical

`ester_debug`:
print to stderr in DEBUG
none else

`ester_info`:
prefixed with green "Info"
print to stdout in both modes

`ester_warn`:
prefixed with yellow "Warning:"
print to stderr
+ indicate line and file in DEBUG

`ester_err`:
same as `ester_warn` with red "Error:"
+ print stack in DEBUG

`ester_critical`:
same as `ester_err` with red "CRITICAL:"
+ exit program or throw eception
*/

#ifdef DEBUG
#define ester_err(...) do { \
LOGE("Error at %s:%d: ", __FILE__, __LINE__); \
LOGE(__VA_ARGS__); \
LOGE("\n"); \
print_stack(); \
throw runtime_exception(); \
#define ester_debug(...) do { \
fprintf(stderr, __VA_ARGS__); \
} while (0)

#define ester_info(...) do { \
printf(GREEN); \
printf("Info: "); \
printf(RESET); \
printf(__VA_ARGS__); \
fflush(stdout); \
} while (0)

#define ester_warn(...) do { \
fprintf(stderr, YELLOW); \
fprintf(stderr, "Warning at %s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while(0)
#else

#define ester_err(...) do { \
LOGE("Error: "); \
LOGE(__VA_ARGS__); \
LOGE("\n"); \
print_stack(); \
exit(EXIT_FAILURE); \
fprintf(stderr, RED); \
fprintf(stderr, "Error at %s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
print_stack();
} while(0)
#endif

#ifdef DEBUG
#define ester_warn(...) do { \
LOGW("Warning at %s:%d: ", __FILE__, __LINE__); \
LOGW(__VA_ARGS__); \
LOGW("\n"); \
#define ester_critical(...) do { \
fprintf(stderr, RED); \
fprintf(stderr, "CRITICAL at %s:%d: ", __FILE__, __LINE__); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
print_stack();
throw runtime_exception(); \
} while(0)
#else

#else // DEBUG
#define ester_debug(...) do {} while (0)

#define ester_info(...) do { \
printf(GREEN); \
printf("Info: "); \
printf(RESET); \
printf(__VA_ARGS__); \
fflush(stdout); \
} while (0)

#define ester_warn(...) do { \
LOGW("Warning: "); \
LOGW(__VA_ARGS__); \
LOGW("\n"); \
fprintf(stderr, YELLOW); \
fprintf(stderr, "Warning: "); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while(0)
#endif

#ifdef DEBUG
#define ester_debug(...) do { \
#define ester_err(...) do { \
fprintf(stderr, RED); \
fprintf(stderr, "Error: "); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
} while (0)
#else
#define ester_debug(...) do {} while (0)
#endif
fprintf(stderr, "\n"); \
} while(0)

bool isHDF5Name(const char *fileName);
#define ester_critical(...) do { \
fprintf(stderr, RED); \
fprintf(stderr, "CRITICAL: "); \
fprintf(stderr, RESET); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(EXIT_FAILURE); \
} while(0)

#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define YELLOW "\033[0;33m"
#define RESET "\033[0;0m"
#endif // DEBUG

#define LOGI(...) { printf(GREEN); printf(__VA_ARGS__); printf(RESET); fflush(stdout); }
#define LOGW(...) { fprintf(stderr, YELLOW); fprintf(stderr, __VA_ARGS__); fprintf(stderr, RESET); }
#define LOGE(...) { fprintf(stderr, RED); fprintf(stderr, __VA_ARGS__); fprintf(stderr, RESET); }
bool isHDF5Name(const char *fileName);

#endif // ESTER_UTILS_H
2 changes: 1 addition & 1 deletion src/main/ester-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ int main(int argc,char *argv[]) {
return 0;
}

ester_err("Unknown file format");
ester_critical("Unknown file format");
return 1;
}
6 changes: 3 additions & 3 deletions src/main/gen_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc,char *argv[]) {
if(!m1d.read(input_file, 1)) output2d(m1d);
else if (!m2d.read(input_file)) output2d(m2d);
else {
ester_err("Error reading input file: %s",input_file);
ester_critical("Error reading input file: %s",input_file);
return 1;
}

Expand Down Expand Up @@ -589,8 +589,8 @@ void write(const star2d &A,char *var,char *fmt) {
fwrite(&d,sizeof(double),1,stdout);
}
} else {
ester_err("Unknown variable %s", var);
exit(1);
ester_critical("Unknown variable %s", var);
exit(1); // Useless because ester_critical already exit
}
}

2 changes: 1 addition & 1 deletion src/main/read_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ int configuration::check_arg(const char *arg,const char *val) {
}

void configuration::missing_argument(const char *arg) {
ester_err("Error: Argument to '%s' missing", arg);
ester_critical("Error: Argument to '%s' missing", arg);
}
2 changes: 1 addition & 1 deletion src/main/star1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc,char *argv[]) {
solver *op;

if(A.init(config.input_file,config.param_file,argc,argv)) {
ester_err("Could not initialize star");
ester_critical("Could not initialize star");
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/star2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main(int argc,char *argv[]) {
solver *op;

if(A.init(config.input_file,config.param_file,argc,argv)) {
ester_err("Could not initialize star");
ester_critical("Could not initialize star");
return 1;
}

Expand Down
12 changes: 4 additions & 8 deletions src/main/star_evol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ int main(int argc,char *argv[]) {
else Xcmin=atof(val);
} else err_code=1;
if(err_code==1) {
fprintf(stderr,"Unknown parameter %s\n",arg);
exit(1);
ester_critical("Unknown parameter %s", arg);
}
if(err_code==2) {
fprintf(stderr,"Argument to %s missing\n",arg);
exit(1);
ester_critical("Argument to %s missing", arg);
}
cmd.ack(arg,val);
}
cmd.close();

if(*config.input_file==0) {
fprintf(stderr,"Must specify an input file\n");
exit(1);
ester_critical("Must specify an input file");
}
if(*config.output_file==0) {
strcpy(config.input_file,config.output_file);
Expand All @@ -53,8 +50,7 @@ int main(int argc,char *argv[]) {
if(A.read(config.input_file)) {
star1d A1d;
if(A1d.read(config.input_file)) {
fprintf(stderr,"Error reading input file %s\n",config.input_file);
exit(1);
ester_critical("Error reading input file %s", config.input_file);
}
A=A1d;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/vtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int main(int argc, char *argv[]) {
fclose(f);
}
else {
fprintf(stderr, "Couldn't open model %s (is is a 2D model?)\n", input_model);
ester_err("Couldn't open model %s (is is a 2D model?)", input_model);
return 1;
}

Expand Down
20 changes: 10 additions & 10 deletions src/mapping/mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ matrix mapping::stream(const matrix &Fz) const {
matrix mapping::eval(const matrix &y,const matrix &ri, const matrix &thi,int parity) const {

if(ri.nrows()!=thi.nrows()||ri.ncols()!=thi.ncols()) {
ester_err("(mapping.eval) Matrix dimensions must agree");
exit(1);
ester_critical("(mapping.eval) Matrix dimensions must agree");
exit(1); // Useless because ester_critical already exit
}
if(y.nrows()!=gl.N||y.ncols()!=leg.npts) {
ester_err("(mapping.eval) Matrix dimensions must agree");
exit(1);
ester_critical("(mapping.eval) Matrix dimensions must agree");
exit(1); // Useless because ester_critical already exit
}

matrix yi(ri.nrows(),ri.ncols());
Expand All @@ -307,9 +307,9 @@ matrix mapping::eval(const matrix &y,const matrix &ri, const matrix &thi,int par
if(zi-1>-1e-10&&zi-1<1e-10) zi=1;
if(zi<1e-10&&zi>-1e-10) zi=0;
if(zi>1||zi<0) {
ester_err("(mapping.eval) Coordinates (r,theta)=(%f,%f) are out of bounds",
ester_critical("(mapping.eval) Coordinates (r,theta)=(%f,%f) are out of bounds",
ri(i),thi(i));
exit(1);
exit(1); // Useless because ester_critical already exit
}
int fin=0,nit=0;
if(zi==0||zi==1) fin=99;
Expand All @@ -319,8 +319,8 @@ matrix mapping::eval(const matrix &y,const matrix &ri, const matrix &thi,int par
if(fabs(dzi)<1e-10) fin++;
nit++;
if(nit>100) {
ester_err("(mapping.eval) Failed to converge");
exit(1);
ester_critical("(mapping.eval) Failed to converge");
exit(1); // Useless because ester_critical already exit
}
zi+=dzi;
}
Expand All @@ -336,8 +336,8 @@ matrix mapping::zeta_to_r(const matrix &z) const {
matrix rr(z.nrows(),z.ncols());

if(z.ncols()!=leg.npts) {
ester_err("(mapping.zeta_to_r) Matrix must have nth columns");
exit(1);
ester_critical("(mapping.zeta_to_r) Matrix must have nth columns");
exit(1); // Useless because ester_critical already exit
}

for(int j=0;j<leg.npts;j++) {
Expand Down
24 changes: 12 additions & 12 deletions src/mapping/remapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void remapper::set_fixed(int idom_new,int idom_old) {

fixed[idom_new]=idom_old;
if(idom_old>map.ndomains) {
ester_err("(remapper) Index exceeds number of domains");
exit(1);
ester_critical("(remapper) Index exceeds number of domains");
exit(1); // Useless because ester_critical already exit
}

}
Expand Down Expand Up @@ -182,18 +182,18 @@ matrix remapper::interp_ex(const matrix &y,int parity) {
void remapper::remap() {

if(ndomains!=map.ndomains&&!changed_npts) {
ester_err("(remapper) Should specify number of points in each domain");
exit(1);
ester_critical("(remapper) Should specify number of points in each domain");
exit(1); // Useless because ester_critical already exit
}

if(ndomains!=map.ndomains&&!redist) {
ester_err("(remapper) Should specify domain boundaries");
exit(1);
ester_critical("(remapper) Should specify domain boundaries");
exit(1); // Useless because ester_critical already exit
}

if(R.nrows()!=ndomains+1) {
ester_err("(remapper) Incorrect size of boundary matrix R");
exit(1);
ester_critical("(remapper) Incorrect size of boundary matrix R");
exit(1); // Useless because ester_critical already exit
}
remapped=1;

Expand All @@ -206,8 +206,8 @@ void remapper::remap() {

if(R.ncols()!=nt) {
if(R.ncols()!=map.nt) {
ester_err("Error: (remapper) Incorrect size of boundary matrix R");
exit(1);
ester_critical("Error: (remapper) Incorrect size of boundary matrix R");
exit(1); // Useless because ester_critical already exit
}
R=map.leg.eval_00(R,map_new.th);
}
Expand Down Expand Up @@ -274,8 +274,8 @@ void remapper::remap() {
zi+=dzi;
nit++;
if(nit>100) {
ester_err("(remapper) No convergence in remap()");
exit(1);
ester_critical("(remapper) No convergence in remap()");
exit(1); // Useless because ester_critical already exit
}
}
zmap.setcol(j,zi);
Expand Down
11 changes: 5 additions & 6 deletions src/matrix/mat_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "utils.h"


matrix cos(const matrix &a) {
Expand Down Expand Up @@ -253,9 +254,8 @@ matrix atan2(const matrix &a,const matrix &b) {
int i,N;

if( (b.nf!=a.nf) || (b.nc!=a.nc) ) {
fprintf(stderr,"ERROR: (matrix.atan2) Dimensions must agree\n");
exit(1);
}
ester_critical("(matrix.atan2) Dimensions must agree");
}

N=a.nc*a.nf;

Expand Down Expand Up @@ -300,9 +300,8 @@ matrix pow(const matrix &a,const matrix &b) {
int i,N;

if( (b.nf!=a.nf) || (b.nc!=a.nc) ) {
fprintf(stderr,"ERROR: (matrix.pow) Dimensions must agree\n");
exit(1);
}
ester_critical("(matrix.pow) Dimensions must agree");
}

N=a.nc*a.nf;

Expand Down
Loading