File: platform_util.h

package info (click to toggle)
megaglest 3.13.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,416 kB
  • sloc: cpp: 144,271; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 22
file content (147 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (6)
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_PLATFORM_PLATFORMUTIL_H_
#define _SHARED_PLATFORM_PLATFORMUTIL_H_

#ifdef WIN32

#include <windows.h>

#endif

#include <string>
#include <stdexcept>
#include "platform_common.h"
#include "leak_dumper.h"

using namespace Shared::PlatformCommon;
using std::string;
using std::exception;

namespace Shared { namespace Platform {

class megaglest_runtime_error : public runtime_error {
protected:
	bool noStackTrace;
public:
    megaglest_runtime_error(const string& __arg,bool noStackTrace=false);

    bool wantStackTrace() const { return !noStackTrace; }
};

#ifndef WIN32
// =====================================================
//	class PlatformExceptionHandler
// =====================================================

class PlatformExceptionHandler {
public:
	static string application_binary;
	static bool disableBacktrace;
	static string getStackTrace();

	virtual ~PlatformExceptionHandler() {}
	void install(string dumpFileName) {}
	virtual void handle()=0;
#if defined(__WIN32__) && !defined(__GNUC__)
	virtual void handle(LPEXCEPTION_POINTERS pointers)=0;
#endif
};

// =====================================================
//	Misc
// =====================================================
void message(string message,bool isNonGraphicalModeEnabled, string writepath);
void exceptionMessage(const exception &excp);

string getCommandLine();

// WINDOWS
#else

// =====================================================
//	class PlatformExceptionHandler
// =====================================================

class PlatformExceptionHandler {
private:
	static PlatformExceptionHandler *thisPointer;

private:
	static LONG WINAPI handler(LPEXCEPTION_POINTERS pointers);
	string dumpFileName;

public:
	static string application_binary;
	static bool disableBacktrace;
	static string getStackTrace();

	void install(string dumpFileName);
	virtual void handle()=0;
#if !defined(__GNUC__)
	virtual void handle(LPEXCEPTION_POINTERS pointers)=0;
#endif
	static string codeToStr(DWORD code);
};

LONG WINAPI UnhandledExceptionFilter2(struct _EXCEPTION_POINTERS *ExceptionInfo);

// =====================================================
//	Misc
// =====================================================
LPWSTR Ansi2WideString(LPCSTR lpaszString);
std::string utf8_encode(const std::wstring &wstr);
std::wstring utf8_decode(const std::string &str);
std::string getRegKey(const std::string& location, const std::string& name);

void message(string message, bool isNonGraphicalModeEnabled,string writepath);
void exceptionMessage(const exception &excp);
string getCommandLine();
void init_win32();
void done_win32();
void ontop_win32(int width, int height);

void CheckPacketThrottling();

// The following is used for stacking tracing for windows based exceptions
#if !defined(_DEBUG) && !defined(__GNUC__)

// easy safe strings
#define MAXSTRLEN 260
typedef char stringType[MAXSTRLEN];

inline void vformatstring(char *d, const char *fmt, va_list v, int len = MAXSTRLEN) { _vsnprintf(d, len, fmt, v); d[len-1] = 0; }
inline char *copystring(char *d, const char *s, size_t len = MAXSTRLEN) { strncpy(d, s, len); d[len-1] = 0; return d; }
inline char *concatstring(char *d, const char *s, size_t len = MAXSTRLEN) { size_t used = strlen(d); return used < len ? copystring(d+used, s, len-used) : d; }

struct stringformatter {
    char *buf;
    stringformatter(char *buf): buf((char *)buf) {}
    void operator()(const char *fmt, ...) {
        va_list v;
        va_start(v, fmt);
        vformatstring(buf, fmt, v);
        va_end(v);
    }
};

#define formatstring(d) stringformatter((char *)d)
#define defformatstring(d) stringType d; formatstring(d)
#define defvformatstring(d,last,fmt) stringType d; { va_list ap; va_start(ap, last); vformatstring(d, fmt, ap); va_end(ap); }

#endif


#endif

}}//end namespace

#endif