Made the debug config file an INI file.

master
Pacman Ghost 7 years ago
parent edc30326c8
commit c8c828a5ef
  1. 58
      MainApp/DebugConfig.cs
  2. 23
      MainApp/MouseDll.cs
  3. 1
      MainApp/MouseInterception.csproj
  4. 11
      MainApp/Program.cs
  5. 21
      MouseDll/api.cpp
  6. 14
      MouseDll/api.hpp
  7. 30
      MouseDll/core.cpp
  8. 92
      MouseDll/debug.cpp
  9. 37
      MouseDll/debug.hpp
  10. 71
      MouseDll/globals.cpp
  11. 30
      MouseDll/globals.hpp
  12. 8
      MouseDll/mouse.vcproj

@ -1,58 +0,0 @@
using System ;
using System.IO ;
using System.Xml ;
using System.Runtime.InteropServices ;
namespace MouseInterception
{
class DebugConfig
{
// IMPORTANT! The definitions here must be kept in sync with their C equivalents in api.hpp
[StructLayout( LayoutKind.Sequential , CharSet=CharSet.Unicode , Pack=1 )]
public struct ApiSettings
{
public string mLogFilename ;
public string mLogging ;
}
private ApiSettings mSettings ;
public ApiSettings settings { get { return mSettings ; } }
public DebugConfig( string fname )
{
// load the DebugConfig
mSettings = new ApiSettings() ;
if ( ! File.Exists( fname ) )
return ;
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings() ;
xmlReaderSettings.IgnoreComments = true ;
xmlReaderSettings.IgnoreProcessingInstructions = true ;
xmlReaderSettings.IgnoreWhitespace = true ;
XmlDocument xmlDoc = new XmlDocument() ;
using ( XmlReader xmlReader = XmlReader.Create( fname , xmlReaderSettings ) )
xmlDoc.Load( xmlReader ) ;
// parse the values
XmlNode debugXmlNode = xmlDoc.SelectSingleNode( "/debug" ) ;
XmlNode xmlNode = debugXmlNode.SelectSingleNode( "logFilename" ) ;
if ( xmlNode != null )
mSettings.mLogFilename = xmlNode.InnerText.Trim() ;
mSettings.mLogging = "" ;
foreach ( XmlNode xn in debugXmlNode.SelectNodes("logging") )
{
foreach( XmlAttribute xa in xn.Attributes )
{
if ( Boolean.Parse( xa.Value ) )
{
if ( mSettings.mLogging.Length > 0 )
mSettings.mLogging += "|" ;
mSettings.mLogging += xa.Name ;
}
}
}
}
}
}

@ -19,7 +19,10 @@ namespace MouseInterception
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string open_api( callbackDelegate deleg , ref DebugConfig.ApiSettings pDebugSettings ) ;
private static extern string open_api(
callbackDelegate deleg ,
[MarshalAs(UnmanagedType.LPWStr)] string pDebugConfigFilename
) ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
@ -36,21 +39,16 @@ namespace MouseInterception
[MarshalAs(UnmanagedType.LPArray)] AppConfig.ApiAction[] pActions , int nActions
) ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string reload_debug_config( ref DebugConfig.ApiSettings pDebugSettings ) ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string run_main_loop( out int pExitFlag ) ;
private bool mIsLoaded = false ;
public MouseDll( callbackDelegate cbDeleg )
public MouseDll( callbackDelegate cbDeleg , string debugConfigFilename )
{
// open the mouse API
DebugConfig.ApiSettings debugSettings = Program.debugConfig.settings ;
string errorMsg = open_api( cbDeleg , ref debugSettings ) ;
string errorMsg = open_api( cbDeleg , debugConfigFilename ) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
mIsLoaded = true ;
@ -89,15 +87,6 @@ namespace MouseInterception
throw new Exception( errorMsg ) ;
}
public void reloadDebugConfig()
{
// reload the debug config
DebugConfig.ApiSettings debugSettings = Program.debugConfig.settings ;
string errorMsg = reload_debug_config( ref debugSettings ) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
}
public void runMainLoop( out int exitFlag )
{
// run the main loop

@ -52,7 +52,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AppConfig.cs" />
<Compile Include="DebugConfig.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>

@ -14,7 +14,6 @@ namespace MouseInterception
private static string mBaseDir ;
private static AppConfig mAppConfig = null ;
private static DebugConfig mDebugConfig = null ;
private static MouseDll mMouseDll = null ;
private static MainForm mMainForm = null ;
@ -55,15 +54,14 @@ namespace MouseInterception
mAppConfig = new AppConfig( fname ) ;
// load the debug config
fname = Path.Combine( Path.GetDirectoryName(fname) , "debug.xml" ) ;
if ( ! File.Exists( fname ) )
fname = getAppRelativePath( System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".debug.xml" ) ;
mDebugConfig = new DebugConfig( fname ) ;
string debugConfigFilename = Path.Combine( Path.GetDirectoryName(fname) , "debug.ini" ) ;
if ( ! File.Exists( debugConfigFilename ) )
debugConfigFilename = getAppRelativePath( System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".debug.ini" ) ;
// initialize
if ( hasConsole )
System.Console.WriteLine( "" ) ;
mMouseDll = new MouseDll( new MouseDll.callbackDelegate(onCallback) ) ;
mMouseDll = new MouseDll( new MouseDll.callbackDelegate(onCallback) , debugConfigFilename ) ;
}
catch( Exception xcptn )
{
@ -151,7 +149,6 @@ namespace MouseInterception
public static void showErrorMsg( string msg ) { MessageBox.Show(msg,APP_NAME,MessageBoxButtons.OK,MessageBoxIcon.Error) ; }
public static AppConfig appConfig { get { return mAppConfig ; } }
public static DebugConfig debugConfig { get { return mDebugConfig ; } }
public static MouseDll mouseDll { get { return mMouseDll ; } }
public static MainForm mainForm { get { return mMainForm ; } }

@ -8,12 +8,12 @@ using namespace std ;
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR
open_api( PCALLBACKFN pCallbackFn , const ApiDebugConfig* pDebugConfig )
open_api( PCALLBACKFN pCallbackFn , const wchar_t* pDebugConfigFilename )
{
// open the API
try
{
openApi( pCallbackFn , pDebugConfig ) ;
openApi( pCallbackFn , pDebugConfigFilename ) ;
return NULL ;
}
catch ( exception& xcptn )
@ -70,23 +70,6 @@ reload_config(
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extern "C" __declspec(dllexport) BSTR
reload_debug_config( const ApiDebugConfig* pDebugConfig )
{
// reload the debug config
try
{
reloadDebugConfig( pDebugConfig ) ;
return NULL ;
}
catch ( exception& xcptn )
{
return SysAllocString( fromUtf8( MAKE_STRING(xcptn) ).c_str() ) ;
}
}
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR

@ -3,7 +3,7 @@
// ---------------------------------------------------------------------
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in App/ApiDebugConfig.cs.
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in AppConfig.cs.
// --- SETTINGS: application settings ---
struct ApiAppConfig
@ -62,14 +62,6 @@ struct ApiAction
int mKeyModifiers ;
} ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct ApiDebugConfig
{
const wchar_t* mpLogFilename ;
const wchar_t* mpLogging ;
} ;
// ---------------------------------------------------------------------
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in MouseDll.cs.
@ -80,7 +72,7 @@ typedef int (__stdcall *PCALLBACKFN)( int callbackType , const char* pCallbackMs
#define CBTYPE_FATAL_ERROR 3
#define CBTYPE_NEW_DEVICE 10
extern void openApi( PCALLBACKFN pCallbackFn , const ApiDebugConfig* pDebugConfig ) ;
extern void openApi( PCALLBACKFN pCallbackFn , const wchar_t* pDebugConfigFilename ) ;
extern void closeApi() ;
extern void reloadConfig(
@ -91,8 +83,6 @@ extern void reloadConfig(
const ApiEvent* pEvents , int nEvents ,
const ApiAction* pActions , int nActions
) ;
extern void reloadDebugConfig( const ApiDebugConfig* pDebugConfig ) ;
extern void runMainLoop( int* pExitFlag ) ;
// ---------------------------------------------------------------------

@ -6,10 +6,10 @@ using namespace std ;
// ---------------------------------------------------------------------
void
openApi( PCALLBACKFN pCallbackFn , const ApiDebugConfig* pDebugConfig )
openApi( PCALLBACKFN pCallbackFn , const wchar_t* pDebugConfigFilename )
{
assert( pCallbackFn != NULL ) ;
assert( pDebugConfig != NULL ) ;
assert( pDebugConfigFilename != NULL ) ;
// check if we are open
if ( ghInterceptionDll != NULL )
@ -18,7 +18,7 @@ openApi( PCALLBACKFN pCallbackFn , const ApiDebugConfig* pDebugConfig )
// initialize
assert( gpCallbackFn == NULL ) ;
gpCallbackFn = pCallbackFn ;
reloadDebugConfig( pDebugConfig ) ;
loadDebugConfig( pDebugConfigFilename ) ;
// load Interception
wchar_t buf[ _MAX_PATH+1 ] ;
@ -138,27 +138,3 @@ reloadConfig(
LOG_MSG( buf.str() ) ;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void
reloadDebugConfig( const ApiDebugConfig* pDebugConfig )
{
assert( pDebugConfig != NULL ) ;
// initialize the log file
const wchar_t* pLogFilename = pDebugConfig->mpLogFilename ;
if ( pLogFilename == NULL )
pLogFilename = L"" ;
if ( _wcsicmp( gLogFilename.c_str() , pLogFilename ) != 0 )
{
if ( gLogFile.is_open() )
gLogFile.close() ;
if ( pLogFilename[0] != L'\0' )
{
gLogFile.open( pLogFilename ) ;
gLogFilename = pLogFilename ;
}
}
initLogging( pDebugConfig->mpLogging ) ;
}

@ -0,0 +1,92 @@
#include <windows.h>
#include <set>
#include <ctime>
#include "debug.hpp"
#include "globals.hpp"
using namespace std ;
// --- LOCAL DATA ------------------------------------------------------
ofstream gLogFile ;
static wstring gLogFilename ;
static StringSet gEnabledLogging ; // FIXME! make this case-insensitive
// ---------------------------------------------------------------------
void
loadDebugConfig( const wchar_t* pDebugConfigFilename )
{
// initialize logging
gEnabledLogging.clear() ;
wchar_t buf[ 4*1024 ] ;
GetPrivateProfileString( L"Logging" , NULL , NULL , buf , ARRAY_SIZE(buf) , pDebugConfigFilename ) ;
for ( const wchar_t* p=buf ; *p != L'\0' ; p+=wcslen(p)+1 )
{
if ( GetPrivateProfileInt( L"Logging" , p , false , pDebugConfigFilename ) )
gEnabledLogging.insert( toUtf8(p) ) ;
}
// initialize the log file
GetPrivateProfileString( L"Logging" , L"LogFilename" , L"" , buf , ARRAY_SIZE(buf) , pDebugConfigFilename ) ;
const wchar_t* pLogFilename = buf ;
if ( _wcsicmp( gLogFilename.c_str() , pLogFilename ) != 0 )
{
if ( gLogFile.is_open() )
gLogFile.close() ;
if ( pLogFilename[0] != L'\0' )
{
gLogFile.open( pLogFilename ) ;
gLogFilename = pLogFilename ;
}
}
}
// ---------------------------------------------------------------------
string
makeLogMsg( const string& msg )
{
// prepare the log message
stringstream buf ;
const char* p = msg.c_str() ;
for ( ; ; )
{
// output the timestamp/tab
if ( p == msg.c_str() )
{
char buf2[80] ;
time_t now = time( NULL ) ;
struct tm tmNow ;
(void) localtime_s( &tmNow , &now ) ;
strftime( buf2 , sizeof(buf2) , "%d/%b/%y %H:%M:%S" , &tmNow ) ;
for ( int i=0 ; buf2[i] != ' ' ; ++i )
buf2[i] = toupper( buf2[i] ) ;
buf << buf2 ;
}
else
buf << " " ;
buf << " | " ;
// output the next line
const char* q = strchr( p , '\n' ) ;
if ( q == NULL )
{
buf << p << endl ;
break ; // nb: this was the last line
}
buf.write( p , q-p ) ;
buf << endl ;
p = q + 1 ;
if ( *p == '\0' )
break ;
}
return buf.str() ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool isLoggingEnabled( const string& s ) { return gEnabledLogging.find(s) != gEnabledLogging.end() ; }

@ -0,0 +1,37 @@
#ifndef DEBUG_HPP
#define DEBUG_HPP
#include <iostream>
#include <fstream>
// ---------------------------------------------------------------------
extern void loadDebugConfig( const wchar_t* pDebugConfigFilename ) ;
// ---------------------------------------------------------------------
#define LOG_MSG( msg ) \
{ \
string _buf_ = makeLogMsg( MAKE_STRING( msg ) ) ; \
cout << _buf_ ; \
cout.flush() ; \
if ( gLogFile.is_open() ) \
{ \
gLogFile << _buf_ ; \
gLogFile.flush() ; \
} \
}
#define LOG_CMSG( c , msg ) \
{ \
if ( isLoggingEnabled( c ) ) \
LOG_MSG( msg ) ; \
}
extern bool isLoggingEnabled( const std::string& ) ;
extern std::string makeLogMsg( const std::string& ) ;
extern std::ofstream gLogFile ;
// ---------------------------------------------------------------------
#endif // DEBUG_HPP

@ -1,5 +1,3 @@
#include <ctime>
#include "globals.hpp"
using namespace std ;
@ -20,9 +18,6 @@ InterceptionDeviceSet gUnknownDevices ;
PCALLBACKFN gpCallbackFn = NULL ;
wstring gLogFilename ;
ofstream gLogFile ;
// ---------------------------------------------------------------------
string
@ -41,72 +36,6 @@ keyModifiersString( int keyModifiers )
// ---------------------------------------------------------------------
static StringSet gLogging ;
void initLogging( const wchar_t* pLogging )
{
// initialize logging
gLogging.clear() ;
if ( pLogging == NULL )
return ;
for ( const wchar_t* p=pLogging ; ; )
{
const wchar_t* q = wcschr( p , L'|' ) ;
if ( q == NULL )
{
gLogging.insert( toUtf8(p) ) ;
break ;
}
gLogging.insert( toUtf8(p,q-p) ) ;
p = q + 1 ;
}
}
bool isLoggingEnabled( const string& s ) { return gLogging.find(s) != gLogging.end() ; }
string
makeLogMsg( const string& msg )
{
// prepare the log message
stringstream buf ;
const char* p = msg.c_str() ;
for ( ; ; )
{
// output the timestamp/tab
if ( p == msg.c_str() )
{
char buf2[80] ;
time_t now = time( NULL ) ;
struct tm tmNow ;
(void) localtime_s( &tmNow , &now ) ;
strftime( buf2 , sizeof(buf2) , "%d/%b/%y %H:%M:%S" , &tmNow ) ;
for ( int i=0 ; buf2[i] != ' ' ; ++i )
buf2[i] = toupper( buf2[i] ) ;
buf << buf2 ;
}
else
buf << " " ;
buf << " | " ;
// output the next line
const char* q = strchr( p , '\n' ) ;
if ( q == NULL )
{
buf << p << endl ;
break ; // nb: this was the last line
}
buf.write( p , q-p ) ;
buf << endl ;
p = q + 1 ;
if ( *p == '\0' )
break ;
}
return buf.str() ;
}
// ---------------------------------------------------------------------
ostream&
operator<<( ostream& os , const exception& xcptn )
{

@ -2,8 +2,7 @@
#define GLOBALS_HPP
#include <windows.h>
#include <iostream>
#include <fstream>
#include <iosfwd>
#include <vector>
#include <set>
@ -12,6 +11,7 @@
#include "deviceConfig.hpp"
#include "api.hpp"
#include "utils.hpp"
#include "debug.hpp"
// ---------------------------------------------------------------------
@ -40,32 +40,6 @@ extern PCALLBACKFN gpCallbackFn ;
// ---------------------------------------------------------------------
#define LOG_MSG( msg ) \
{ \
string _buf_ = makeLogMsg( MAKE_STRING( msg ) ) ; \
cout << _buf_ ; \
cout.flush() ; \
if ( gLogFile.is_open() ) \
{ \
gLogFile << _buf_ ; \
gLogFile.flush() ; \
} \
}
#define LOG_CMSG( c , msg ) \
{ \
if ( isLoggingEnabled( c ) ) \
LOG_MSG( msg ) ; \
}
extern void initLogging( const wchar_t* ) ;
extern bool isLoggingEnabled( const std::string& ) ;
extern std::string makeLogMsg( const std::string& ) ;
extern std::wstring gLogFilename ;
extern std::ofstream gLogFile ;
// ---------------------------------------------------------------------
extern std::ostream& operator<<( std::ostream& os , const std::exception& xcptn ) ;
// ---------------------------------------------------------------------

@ -198,6 +198,10 @@
RelativePath=".\core2.cpp"
>
</File>
<File
RelativePath=".\debug.cpp"
>
</File>
<File
RelativePath=".\device.cpp"
>
@ -244,6 +248,10 @@
RelativePath=".\appProfile.hpp"
>
</File>
<File
RelativePath=".\debug.hpp"
>
</File>
<File
RelativePath=".\device.hpp"
>

Loading…
Cancel
Save