Flow123d  last_with_con_2.0.0-663-gd0e2296
system.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file system.hh
15  * @brief
16  */
17 
18 #ifndef SYSTEM_H
19 #define SYSTEM_H
20 
21 
22 #include <mpi.h>
23 #include <iostream>
24 
25 #include "system/global_defs.h"
26 #include "system/exc_common.hh"
27 
28 
29 #ifndef _BSD_SOURCE
30  #define _BSD_SOURCE
31 #endif
32 
33 #ifdef WINDOWS
34  #include <direct.h>
35  #define GetCurrentDir _getcwd
36  #define PATH_MAX MAX_PATH
37 #else
38  #include <unistd.h>
39  #define GetCurrentDir getcwd
40  #include <limits.h> //PATH_MAX
41 #endif
42 
43 #define strcmpi strcasecmp
44 #define DIR_DELIMITER '/'
45 
46 
47 // Assuming all compilers supports CXX11 features
48 #define OPERATOR_NEW_THROW_EXCEPTION
49 
50 
51 
52 using namespace std;
53 
54 
55 // **************************************************************
56 /*! @brief Identifiers for various output messages.
57  */
58 typedef enum MessageType {
60 } MessageType;
61 
62 
63 // **************************************************************
64 /*! @brief System structure for various global variables.
65  */
66 typedef struct SystemInfo {
67  int verbosity; // system printf verbosity
68  int pause_after_run; // to keep terminal open on Windows
69  string log_fname; // name of the master log file
70  FILE *log; // log file handle
71 
72  int n_proc; // number of processors
73  int my_proc; // self processor number
74 
76 
77 } SystemInfo;
78 
79 extern SystemInfo sys_info;
80 
81 /*
82 char * get_log_fname( void );
83 char * get_log_file( void );
84 void resume_log_file( void );
85 */
86 
87 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
88 
89 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
90 //void * xmalloc(size_t size);
91 //void * xrealloc( void * ptr, size_t size );
92 
93 // TODO: implement as a templated function
94 /*
95 #ifndef xfree
96  #define xfree(p) \
97  do { if (p) { free(p); (p)=NULL; } \
98  } while (0) /// test & free memory
99 #endif
100 */
101 /**
102  * @brief Replacement of new/delete operator in the spirit of xmalloc.
103  *
104  * Up to my knowledge overloading of original new/delete is the only clean.
105  * Possibly disadvantage is that all 'new' calls in system and other templates
106  * become also overloaded.
107  *
108  */
109 // @{
110 /*
111 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
112 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
113 void operator delete( void *p) throw();
114 void operator delete[]( void *p) throw();
115 */
116 // @}
117 
118 //int xsystem(const char*);
119 
120 //! @brief Operations on files and directories
121 /// @{
122 /*
123 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
124 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
125 int xremove( const char *s ); ///< Remove file or directory (function)
126 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
127 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
128 */
129 //! @}
130 
131 // string operations
132 /*
133 char * xstrcpy(const char*);
134 char * xstrtok(char *s, int position = -1);
135 char * xstrtok(char*,const char* delim, int position = -1);
136 int xchomp( char * s );
137 */
138 
139 /**
140  * Wrapper to check return codes of C functions. In particular PETSC calls.
141  */
142 inline void chkerr(unsigned int ierr) {
143  do {
144  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
145  } while (0);
146 }
147 
148 /**
149  * Wrapper to check return codes of C functions. In particular PETSC calls.
150  * This version do the check just as an debugging assert. So the code is empty
151  * in release version.
152  */
153 inline void chkerr_assert(unsigned int ierr) {
154 #ifdef FLOW123D_DEBUG_ASSERTS
155  chkerr(ierr);
156 #endif
157 }
158 
159 #endif
160 //-----------------------------------------------------------------------------
161 // vim: set cindent:
int my_proc
Definition: system.hh:73
Definition: system.hh:59
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:70
int verbosity
Definition: system.hh:67
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:142
Definition: system.hh:59
System structure for various global variables.
Definition: system.hh:66
string log_fname
Definition: system.hh:69
Global macros to enhance readability and debugging, general constants.
Definition: system.hh:59
void chkerr_assert(unsigned int ierr)
Definition: system.hh:153
Definition: system.hh:59
SystemInfo sys_info
Definition: system.cc:41
Definition: system.hh:59
MessageType
Identifiers for various output messages.
Definition: system.hh:58
Definition: system.hh:59
int _xprintf(const char *const xprintf_file, const char *const xprintf_func, const int xprintf_line, MessageType type, const char *const fmt,...)
Multi-purpose printing routine: messages, warnings, errors.
Definition: system.cc:85
int n_proc
Definition: system.hh:72
Definition: format.cc:82
Definition: system.hh:59
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
MPI_Comm comm
Definition: system.hh:75
int pause_after_run
Definition: system.hh:68