XPPlanes
X-Plane plugin to display additional planes based on network data
Utilities.h
Go to the documentation of this file.
1 
21 #pragma once
22 
23 //
24 // MARK: General texts
25 //
26 
27 #define ERR_ASSERT "ASSERT FAILED: %s"
28 #define ERR_EXCEPTION "EXCEPTION CAUGHT: %s"
29 
30 //
31 // MARK: Misc
32 //
33 
35 float GetMiscNetwTime ();
36 
42 bool CheckEverySoOften (float& _lastCheck, float _interval, float _now);
43 
48 inline bool CheckEverySoOften (float& _lastCheck, float _interval)
49 { return CheckEverySoOften(_lastCheck, _interval, GetMiscNetwTime()); }
50 
52 std::string GetPluginName (XPLMPluginID who);
53 
55 std::istream& safeGetline(std::istream& is, std::string& t);
56 
58 inline double WeatherAltCorr_ft (double pressureAlt_ft, double hPa)
59 { return pressureAlt_ft + ((hPa - HPA_STANDARD) * FT_per_HPA); }
60 
62 float HeadDiff (float from, float to);
63 
69 float RpmToAngle (float angle, float rpm, float s);
70 
72 template<typename T>
73 inline T NZ (T v)
74 { return std::isnan(v) ? T(0) : v; }
75 
76 //
77 // MARK: String functions
78 //
79 
82 std::string str_n (const char* s, size_t max);
83 
85 #define STR_N(s) str_n(s,sizeof(s))
86 
88 std::vector<std::string> str_tokenize (const std::string& s,
89  const std::string& tokens,
90  bool bSkipEmpty = true);
91 
93 class StrTokens {
94 protected:
95  const std::string& s;
96  std::string sep;
97  size_t p = std::string::npos;
98  int num = 0;
99 public:
101  StrTokens (const std::string& _s, const std::string& _sep) : s(_s), sep(_sep) {}
103  std::string next();
105  std::string next(const std::string& _sep) { sep = _sep; return next(); }
107  int count() const { return num; }
109  bool finished () const { return p != std::string::npos && p >= s.length(); }
110 };
111 
113 std::pair<std::string,std::string> str_split (const std::string& s,
114  const std::string& tokens);
115 
116 //
117 // MARK: Logging Support
118 //
119 
122 #if defined(__clang__) || defined(__GNUC__)
123 #define XPPLANES_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1)))
124 #else
125 #define XPPLANES_FMTARGS(FMT)
126 #endif
127 
129 enum logLevelTy : int {
130  logDEBUG = 0,
135  logMSG
136 };
137 
139 const char* LogGetString ( const char* szFile, int ln, const char* szFunc, logLevelTy lvl, const char* szMsg, va_list args );
140 
142 void LogMsg ( const char* szFile, int ln, const char* szFunc, logLevelTy lvl, const char* szMsg, ... ) XPPLANES_FMTARGS(5);
143 
144 //
145 // MARK: Logging macros
146 //
147 
151 #define LOG_MSG(lvl,...) { \
152  if (lvl >= glob.logLvl) \
153  {LogMsg(__FILE__, __LINE__, __func__, lvl, __VA_ARGS__);} \
154 }
155 
159 #define THROW_ERROR(...) \
160 throw XPMP2::XPMP2Error(__FILE__, __LINE__, __func__, __VA_ARGS__);
161 
164 #define LOG_ASSERT(cond) \
165  if (!(cond)) { \
166  THROW_ERROR(ERR_ASSERT,#cond); \
167  }
168 
169 //
170 // MARK: Compiler differences
171 //
172 
173 // XCode/Linux don't provide the _s functions, not even with __STDC_WANT_LIB_EXT1__ 1
174 #if APL
175 inline int strerror_s( char *buf, size_t bufsz, int errnum )
176 { return strerror_r(errnum, buf, bufsz); }
177 #elif LIN
178 inline int strerror_s( char *buf, size_t bufsz, int errnum )
179 { strerror_r(errnum, buf, bufsz); return 0; }
180 #endif
181 
182 // MARK: Thread names
183 #ifdef DEBUG
184 // This might not work on older Windows version, which is why we don't publish it in release builds
185 #if IBM
186 #define SET_THREAD_NAME(sName) SetThreadDescription(GetCurrentThread(), L##sName)
187 #elif APL
188 #define SET_THREAD_NAME(sName) pthread_setname_np(sName)
189 #elif LIN
190 #define SET_THREAD_NAME(sName) pthread_setname_np(pthread_self(),sName)
191 #endif
192 #else
193 #define SET_THREAD_NAME(sName)
194 #endif
constexpr double FT_per_HPA
ft altitude diff per hPa change
Definition: Constants.h:31
constexpr double HPA_STANDARD
Definition: Constants.h:26
#define XPPLANES_FMTARGS(FMT)
To apply printf-style warnings to our functions.
Definition: Utilities.h:125
float HeadDiff(float from, float to)
Return shortest turn from one heading to the other.
Definition: Utilities.cpp:75
float RpmToAngle(float angle, float rpm, float s)
Rotation: Computes new rotation angle based on current + revolution in a (small) amount of time.
Definition: Utilities.cpp:85
const char * LogGetString(const char *szFile, int ln, const char *szFunc, logLevelTy lvl, const char *szMsg, va_list args)
Returns ptr to static buffer filled with formatted log string.
Definition: Utilities.cpp:180
float GetMiscNetwTime()
Get synched network time from X-Plane (sim/network/misc/network_time_sec) as used in Log....
Definition: Utilities.cpp:28
void LogMsg(const char *szFile, int ln, const char *szFunc, logLevelTy lvl, const char *szMsg,...) XPPLANES_FMTARGS(5)
Log Text to log file.
Definition: Utilities.cpp:224
std::string GetPluginName(XPLMPluginID who)
Return a plugin's name.
Definition: Utilities.cpp:51
std::vector< std::string > str_tokenize(const std::string &s, const std::string &tokens, bool bSkipEmpty=true)
separates string into tokens
Definition: Utilities.cpp:113
std::istream & safeGetline(std::istream &is, std::string &t)
Read a text line, handling both Windows (CRLF) and Unix (LF) ending.
Definition: Utilities.cpp:62
double WeatherAltCorr_ft(double pressureAlt_ft, double hPa)
Compute geometric altitude [ft] from pressure altitude and current weather in a very simplistic manne...
Definition: Utilities.h:58
T NZ(T v)
Replace NAN with 0.0.
Definition: Utilities.h:73
std::pair< std::string, std::string > str_split(const std::string &s, const std::string &tokens)
Split the string at the first of the tokens and return the two pieces.
Definition: Utilities.cpp:155
std::string str_n(const char *s, size_t max)
Copy at most n chars from location, or less if zero-terminated.
Definition: Utilities.cpp:104
logLevelTy
Logging level.
Definition: Utilities.h:129
@ logDEBUG
Debug, highest level of detail.
Definition: Utilities.h:130
@ logINFO
regular info messages
Definition: Utilities.h:131
@ logWARN
warnings, i.e. unexpected, but uncritical events, maybe leading to unwanted display,...
Definition: Utilities.h:132
@ logFATAL
fatal is shortly before a crash
Definition: Utilities.h:134
@ logERR
errors mean, aircraft can potentially not be displayed
Definition: Utilities.h:133
@ logMSG
will always be output, no matter what has been configured, cannot be suppressed
Definition: Utilities.h:135
bool CheckEverySoOften(float &_lastCheck, float _interval, float _now)
Convenience function to check on something at most every x seconds.
Definition: Utilities.cpp:40
Class to extract tokens from a string.
Definition: Utilities.h:93
std::string next(const std::string &_sep)
(re)sets the separators, then returns the next token
Definition: Utilities.h:105
StrTokens(const std::string &_s, const std::string &_sep)
Constructor: Pass in the string to separate and the separators.
Definition: Utilities.h:101
bool finished() const
have all tokens been returned?
Definition: Utilities.h:109
const std::string & s
reference to the string to search
Definition: Utilities.h:95
size_t p
points to last separator found
Definition: Utilities.h:97
std::string sep
separators
Definition: Utilities.h:96
int count() const
returns how many finds have been returned so far
Definition: Utilities.h:107
std::string next()
returns the next token, can be the empty string if two tokens follow immediately, or if finished()
Definition: Utilities.cpp:136
int num
counts the findings
Definition: Utilities.h:98