Skip to content
This repository has been archived by the owner on Jan 29, 2018. It is now read-only.

Commit

Permalink
Added debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
qtnc committed Apr 24, 2015
1 parent 3e6c381 commit a8a1ae3
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 17 deletions.
33 changes: 33 additions & 0 deletions 6pad.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
6pad log
Init paths...
Window class name = QC6Pad01_a
appName = C:\MinGW\ProgQ\9pad++
appDir = a
config file = C:\MinGW\ProgQ\9pad++\a.ini
Loading config file...
Loading language file...
Loading argvlist...
argv[0]=a
Parsing argvlist...
firstInstance = true
Registering window class...
Registering common controls...
Init app window GUI...
Loading recent files list...
Init python...
Parsing argvlist again...
Begin PyStart...
Reloading last opened files...
Set python path...
Registering sixpad python module...
Calling Py_Initialize...
Show app window and starting event loop...
Init python GIL...
Entering python GIL...
Running builtin python script...
Running auto python script C:\MinGW\ProgQ\9pad++\a.py...
Init extensions...
Python ready, starting interactive console...
End of message loop
Saving config...
Finished, exiting with code 0
16 changes: 15 additions & 1 deletion Py6padModule.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "global.h"
#include "strings.hpp"
#include "IniFile.h"
#include "File.h"
#include "python34.h"
#include "Resource.h"
#include "Thread.h"
using namespace std;

extern IniFile config;
extern File dbg;
extern tstring appPath, appDir, appName, configFileName;
extern vector<tstring> argv;

Expand Down Expand Up @@ -114,13 +116,21 @@ return mod;
}

void PyStart (void) {
if (DEBUG) dbg << "Begin PyStart...\r\n";
wstring modulePath = toWString( appDir + TEXT("\\python34.zip;") + appDir + TEXT("\\lib") );
if (DEBUG) dbg <<"Set python path...\r\n";
Py_SetPath( modulePath.c_str() );
Py_SetProgramName(const_cast<wchar_t*>(toWString(argv[0]).c_str()));
if (DEBUG) dbg << "Registering sixpad python module...\r\n";
PyImport_AppendInittab("sixpad", PyInit_6padMain);;
if (DEBUG) dbg << "Calling Py_Initialize...\r\n";
Py_Initialize();
PySys_SetPath( toTString( appDir + TEXT("\\python34.zip;") + appDir + TEXT("\\lib") ).c_str() );
if (DEBUG) dbg <<"Init python GIL...\r\n";
PyEval_InitThreads();
if (DEBUG) dbg <<"Entering python GIL...\r\n";
GIL_PROTECT
{
if (DEBUG) dbg << "Running builtin python script...\r\n";
Resource res(TEXT("init.py"),257);
char* code = (char*)res.copy();
bool failed = !!PyRun_SimpleString(code);
Expand All @@ -129,14 +139,18 @@ if (failed) exit(1);
}
RunSync([](){});//Barrier to wait for the main loop to start
string pyfn = toString(appDir + TEXT("\\") + appName + TEXT(".py"));
if (DEBUG) dbg << "Running auto python script " << pyfn << "...\r\n";
PyInclude(pyfn);
if (DEBUG) dbg << "Init extensions...\r\n";
for (auto it = config.find("extension"); it!=config.end(); ++it) {
string name = it->second;
if (DEBUG) dbg << "Loading extension: " << name << "...\r\n";
if (endsWith(name, ".py")) PyInclude(name);
else if (endsWith(name, ".dll")) {}//C++ extension, not yet supported
else PyImport_ImportModule(name.c_str());
}
// Ohter initialization stuff goes here
if (DEBUG) dbg << "Python ready, starting interactive console...\r\n";

// From now on, make this thread sleep forever
// For a yet unknown reason, if we don't do this, the python console window hangs
Expand Down
2 changes: 1 addition & 1 deletion a.lng
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Sa&ve as...=Enregistrer &sous...
E&xit=&Quitter
&Edit=Edition
&Undo=&Annuler
Re&do=Re&faire
Re&do=Réta&blir
&Copy=&Copier
C&ut=Co&uper
&Paste=Co&ller
Expand Down
2 changes: 2 additions & 0 deletions a.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ def func () :

window.menus.edit.add(label='Item inutile', accelerator='Ctrl+E', action=func)


print(sys.path)
7 changes: 7 additions & 0 deletions file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ if (fd&&!noclose) CloseHandle(fd);
fd=0;
}

void File::flush () {
if (fd) FlushFileBuffers(fd);
}

bool File::open (const tstring& path, bool write) {
if (path==TEXT("STDIN")) { fd = GetStdHandle(STD_INPUT_HANDLE); noclose=true; }
else if (path==TEXT("STDOUT")) fd = GetStdHandle(STD_OUTPUT_HANDLE);
Expand All @@ -21,13 +25,15 @@ return fd!=INVALID_HANDLE_VALUE;
}

int File::read (void* buf, int len) {
if (!fd) return -1;
DWORD nRead=0;
if (ReadFile(fd, buf, len, &nRead, NULL)) return nRead;
else { close(); return -1; }
}

int File::write (const void* buf, int len) {
DWORD nWritten=0;
if (!fd) return -1;
if (len<0) len = strlen((const char*)buf);
if (WriteFile(fd, buf, len, &nWritten, NULL)) return nWritten;
else { close(); return -1; }
Expand Down Expand Up @@ -55,6 +61,7 @@ return str;

bool File::writeFully (const void* buf, int len) {
DWORD pos=0, nWritten=0;
if (!fd) return false;
while (pos<len && WriteFile(fd, buf+pos, len-pos, &nWritten, NULL)) pos+=nWritten;
return pos>=len;
}
Expand Down
5 changes: 5 additions & 0 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ string readFully () ;
string readUntil (char c = '\n', char ign = '\r') ;
string readLine () { return readUntil(); }
void close () ;
void flush () ;
~File();
operator bool () ;

inline File& operator<< (const string& s) { write(s); flush(); return *this; }
inline File& operator<< (const char* s) { return operator<<(string(s)); }
template<class T> inline File& operator<< (const T& x) { return operator<<(toString(x)); }
};

#endif
4 changes: 4 additions & 0 deletions global.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ typedef std::string tstring;
#define RELEASE 0
#endif

#ifndef DEBUG
#define DEBUG 1
#endif

#define SIXPAD_VERSION "Alpha 1"

#define LE_DOS 0
Expand Down
66 changes: 55 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include<list>
#include<unordered_map>
#include<functional>
#include<csignal>
using namespace std;

#define OF_REUSEOPENEDTABS 1
Expand All @@ -19,6 +20,7 @@ using namespace std;

tstring appPath, appDir, appName, configFileName;
IniFile msgs, config;
File dbg;
eventlist listeners;
shared_ptr<Page> curPage(0);
list<tstring> consoleInput;
Expand Down Expand Up @@ -438,17 +440,32 @@ data += 2+strlen(str);
SetMenuName(menu, cmd, cmd<1000, toTString(str).c_str() );
}}

static void termHandler (void) {
if (DEBUG) dbg << "terminate called.\r\n";
if (win) MessageBox(win, msg("A fatal error occurred and the application must be terminated. Please excuse for the inconvenience.").c_str(), msg("Fatal error").c_str(), MB_OK | MB_ICONERROR);
exit(3);
}

static void sigsegv (int exCode) {
if (DEBUG) dbg << "Uncaught signal: " << exCode << "\r\n";
std::terminate();
}

extern "C" int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nWindowStile) {
hinstance = hThisInstance;
if (DEBUG) dbg.open(TEXT("6pad.log"), true);
long long time = GetTickCount();
hinstance = hThisInstance;
if (DEBUG) dbg << "6pad log\r\n";

//signal(SIGSEGV, sigsegv);
//signal(SIGFPE, sigsegv);
//signal(SIGILL, sigsegv);
set_terminate(termHandler);
set_unexpected(termHandler);
signal(SIGSEGV, sigsegv);
signal(SIGFPE, sigsegv);
signal(SIGILL, sigsegv);
SetErrorMode(SEM_FAILCRITICALERRORS);


{//Getting paths and such global parameters
if (DEBUG) dbg << "Init paths...\r\n";
TCHAR fn[512]={0};
GetModuleFileName(NULL, fn, 511);
appPath = fn;
Expand All @@ -460,16 +477,27 @@ tsnprintf(CLASSNAME, sizeof(CLASSNAME)/sizeof(TCHAR), TEXT("QC6Pad01_%s"), fnBs+
appName = fnBs+1;
appDir = fn;
configFileName = appDir + TEXT("\\") + appName + TEXT(".ini");
if (DEBUG) dbg << snsprintf(512, string("Window class name = %ls\r\nappName = %ls\r\nappDir = %ls\r\nconfig file = %ls\r\n"), CLASSNAME, appDir.c_str(), appName.c_str(), configFileName.c_str() );
if (DEBUG) dbg << "Loading config file...\r\n";
config.load(configFileName);
if (DEBUG) dbg << "Loading language file...\r\n";
msgs.load(appDir + TEXT("\\") + appName + TEXT(".lng") );
}

{//Loading command line parameters
if (DEBUG) dbg << "Loading argvlist...\r\n";
int argc=0;
wchar_t** args = CommandLineToArgvW(GetCommandLineW(),&argc);

wchar_t* argline = GetCommandLineW();
if (argline) {
wchar_t** args = CommandLineToArgvW(argline, &argc);
if (args) {
for (wchar_t** arg = args; *arg; ++arg) argv.push_back(toTString(*arg));
if (DEBUG) for(int i=0, n=argv.size(); i<n; i++) dbg << snsprintf(512, "argv[%d]=%ls\r\n", i, argv[i].c_str() );
LocalFree(args);
}
}}}
if (DEBUG) dbg << "Parsing argvlist...\r\n";
if (argv.size()<=0) argv.push_back(appPath);
for (int i=1; i<argv.size(); i++) {
tstring arg = argv[i];
if (arg.size()<=0) continue;
Expand All @@ -479,11 +507,16 @@ if (arg==TEXT("/stdout")) writeToStdout=true;
else if (arg==TEXT("/headless")) headless=true;
continue;
}
if (OpenFile2(arg, OF_REUSEOPENEDTABS)) return 0;
}
if (DEBUG) dbg << "Checking open state of " << arg << "...\r\n";
if (OpenFile2(arg, OF_REUSEOPENEDTABS)) {
if (DEBUG) dbg << arg << " is already open in another instance, exiting\r\n";
return 0;
}}
firstInstance = !FindWindow(CLASSNAME,NULL);
if (DEBUG) dbg << "firstInstance = " << firstInstance << "\r\n";

{//Register class and controls block
if (DEBUG) dbg << "Registering window class...\r\n";
WNDCLASSEX wincl;
wincl.hInstance = hinstance;
wincl.lpszClassName = CLASSNAME;
Expand All @@ -498,16 +531,18 @@ wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx(&wincl)) {
MessageBox(NULL, TEXT("Couldn't register window class"), TEXT("Fatal error"), MB_OK|MB_ICONERROR);
if (DEBUG) dbg << "Couldn't register window class, exiting\r\n";
return 1;
}
if (DEBUG) dbg << "Registering common controls...\r\n";
INITCOMMONCONTROLSEX ccex = { sizeof(INITCOMMONCONTROLSEX), ICC_BAR_CLASSES | ICC_PROGRESS_CLASS | ICC_TAB_CLASSES | ICC_COOL_CLASSES | ICC_TREEVIEW_CLASSES | ICC_LISTVIEW_CLASSES };
if (!InitCommonControlsEx(&ccex)) {
MessageBox(NULL, TEXT("Couldn't initialize common controls"), TEXT("Fatal error"), MB_OK|MB_ICONERROR);
if (DEBUG) dbg << "Couldn't initialize common controls, exiting\r\n";
return 1;
}}

{//Create window block
if (DEBUG) dbg << "Init app window GUI...\r\n";
win = CreateWindowEx(
WS_EX_CONTROLPARENT | WS_EX_ACCEPTFILES,
CLASSNAME, TEXT("6Pad++ ") TEXT(SIXPAD_VERSION),
Expand Down Expand Up @@ -541,24 +576,29 @@ SetMenuNamesFromResource(menu);
}

{//Recent files
if (DEBUG) dbg << "Loading recent files list...\r\n";
for (int i=0; config.contains("recentFile"+toString(i)); i++) {
recentFiles.push_back(config.get<tstring>("recentFile"+toString(i), TEXT("") ));
}
UpdateRecentFilesMenu();
}//END Recentfiles

if (DEBUG) dbg << "Init python...\r\n";
consoleInputEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
InitializeCriticalSection(&csConsoleInput);
Thread::start(PyStart);

if (DEBUG) dbg << "Parsing argvlist again...\r\n";
for (int i=1; i<argv.size(); i++) {
const tstring& arg = argv[i];
if (arg.size()<=0) continue;
else if (arg[0]=='-' || arg[0]=='/') { continue; } // options
if (DEBUG) dbg <<"Opening " << arg << "...\r\n";
OpenFile(arg);
}

if (firstInstance) {//Reload last opened files
if (DEBUG) dbg << "Reloading last opened files...\r\n";
int mode = config.get("reloadLastFilesMode",0);
if (mode==1 && pages.size()>0) mode=0;
for (int i=0; config.contains("lastFile" + toString(i)); i++) {
Expand All @@ -575,6 +615,7 @@ if (pages.size()<=0) PageAddEmpty(false);
time = GetTickCount() -time;
printf("Init time = %d ms\r\n", time);

if (DEBUG) dbg << "Show app window and starting event loop...\r\n";
if (headless) PostQuitMessage(0);
else ShowWindow(win, nWindowStile);
PageActivated(pages[0]);
Expand All @@ -589,10 +630,12 @@ DispatchMessage(&msg);
endmsgloop: ;
}

if (DEBUG) dbg << "End of message loop\r\nSaving config...\r\n";
{int i=0; for(const tstring& file: recentFiles) {
config.set("recentFile" + toString(i++), file);
}}
if (config.size()>0) config.save(appDir + TEXT("\\") + appName + TEXT(".ini") );
if (DEBUG) dbg << "Finished, exiting with code " << msg.wParam;
return msg.wParam;
}

Expand Down Expand Up @@ -657,6 +700,7 @@ case IDM_NEXT_MODLESS: GoToNextModlessWindow(1); return true;
case IDM_PREV_MODLESS: GoToNextModlessWindow(-1); return true;
case IDM_ABOUT: AboutDlg(); break;
case IDM_EXIT: SendMessage(win, WM_CLOSE, 0, 0); return true;
case IDM_CRASH: { int* p=0; *p=0; } return true; // Force crash
}
if (cmd>=IDM_ENCODING && cmd<IDM_ENCODING+encodings.size() ) {
PageSetEncoding(curPage, cmd-IDM_ENCODING);
Expand Down
2 changes: 1 addition & 1 deletion pycompile.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@echo off
cd lib
for /r %%i in (*.py) do python -O ..\pycompile.py %%i
for /r %%i in (*.py) do isnewer %%i %%io && python -O ..\pycompile.py %%i
zip -9 -u -q -r ..\python34.zip *.pyo *\*.pyo *\*\*.pyo *\*\*\*.pyo
cd ..
2 changes: 1 addition & 1 deletion r.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@echo off
for %%i in (*.cpp) do g++ -c -s -O3 -std=gnu++11 %%i -w -o obj\%%~ni.o -DRELEASE
windres res.rc -o obj\rc.o
g++ -o 6pad++.exe -s -O3 -w obj\*.o -luser32 -lkernel32 -lcomdlg32 -lcomctl32 -lgdi32 -lwinmm -lboost-regex -L. -lpython34 -mthreads -m32 -DRELEASE
g++ -o 6pad++.exe -s -O3 -w obj\*.o -luser32 -lkernel32 -lcomdlg32 -lcomctl32 -lgdi32 -lwinmm -lboost-regex -L. -lpython34 -mthreads -mwindows -m32 -DRELEASE
rem -static-libstdc++ -static-libgcc
upx 6pad++.exe *.dll
del obj\*.o
Expand Down
2 changes: 1 addition & 1 deletion strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool endsWith (const string& a, const string& b) {
return boost::ends_with(a,b);
}

string snprintf (int max, const string& fmt, ...) {
string snsprintf (int max, const string& fmt, ...) {
string out(max+1, '\0');
va_list ap;
va_start(ap,fmt);
Expand Down
Loading

0 comments on commit a8a1ae3

Please sign in to comment.