-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgexception.cpp
More file actions
2506 lines (2321 loc) · 82.2 KB
/
Copy pathMsgexception.cpp
File metadata and controls
2506 lines (2321 loc) · 82.2 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2000-2011, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//
// P2Pevent implementation
//
#include "stdafx.h"
#include "afxwin.h"
#include <sys/timeb.h> // was <sys\timeb.h> — backslash is non-portable (MSVC accepts /)
#include <cstdlib> // getenv, for the P2PMSG_NO_UI knob (AFTER stdafx.h: /Yu drops anything before it)
#include <cstdio> // fopen/fwprintf, for the configured log file
#include <ctime> // localtime_s / localtime_r, for the log timestamp
#include "Msgcore.h"
//#include "P2Pwin32.h"
#include "Msgexception.h"
#include "MsgList.h"
#include "MsgCurs.h"
///////////////////////////////////////////////////////////////////////
// Constructors and destructor
static WCHAR s_szServiceName[32] = { 0 };
static DWORD s_nEventNo = 0;
// THE DEFAULT NOTIFICATION MASK. ERROR and WARNING.
// NOTES: WARNING joined it on 2026-08-20, closing Targetcore's F-S6-4.
// Until then this was P2Pevotn_ERROR alone, which meant every
// EVWRN->Display() in these repositories was raised into nothing
// on every build - the diagnostic ran, formatted its text and its
// advice, and returned without emitting. A warning nobody can see
// does not warn anybody, and the three that exist are all operator
// MISCONFIGURATIONS with a fix attached:
// - a relay attestation that could not be signed;
// - a peer signing logins to a hub that does not require them;
// - P2PeerioBSTR, which carries cleartext, being constructed.
// : The usual argument against warnings on by default is flooding.
// It does not apply here and that is not luck: every one of those
// three ends in ->Display()->SetLast(), which reports once per
// connection rather than once per message, and each was written
// that way deliberately. A misconfigured hub costs one line.
// : It is a behaviour change for every existing host, which is why it
// is a decision of its own and not a rider on the fix that found it.
// The consuming project's versioning policy carries it as an
// unreleased break rather than a silent one. An application
// that does not want them says so in one call, the same way it
// always could: P2Pevent::Configure(P2PeventCfg_REMMASK,
// P2Pevotn_WARNING);
static DWORD s_dwP2Pevotn_MASK = ( P2Pevotn_ERROR | P2Pevotn_WARNING );
UINT WM_P2PeventNOTN = WM_APP + 0x01E0;
#pragma pack(push,1)
typedef struct // P2PeventNode details
{
BYTE eClass; // Event class
time_t dwEpoch; // Event epoch
DWORD nEventNo; // Event number
HRESULT hr;
} P2PeventNode;
#pragma pack(pop)
//
// Parameters: const P2Pevent& rhs
// Self reference
//
P2Pevent::P2Pevent ( )
: P3PmsgItem ( L"P2Pevent", AllocBLOB08(sizeof(P2PeventNode) ) )
{
}
P2Pevent::P2Pevent ( const P2Pevent& rhs )
{
*this = rhs;
}
P2Pevent::P2Pevent ( const P3PmsgItem& rhs )
: P3PmsgItem ( rhs )
{
}
P2Pevent::~P2Pevent ( )
{
// Last events maintenance
if ( GetP2Pevent() == this )
IsolateP2Pevent ( );
}
//
// Cancels the contained P2Pevent
// NOTES: Object self destructs. P2Pevent's are intended to
// accumulate information over their life cycle. As such
// notifications are performed immediately before cancellation
//
// Parameters: bool bNotify = true
// Notifications flag
// true... Perform notifications
// false.. Skip all notifications
//
// Returns: P2Pevent* = 0
// Null pointer
P2Pevent*
P2Pevent::Cancel ( bool bNotify )
{
// Confirm
if ( bNotify &&
!m_bDisplayed )
Display();
// Perform notifications
P2PeventPost_HWND ( *this );
// Simply
delete this;
return 0;
}
//
// Registers for external P2Pevent notifications
// NOTES: Registered notifications are subsequently performed for
// each matching type.
//
// Parameters: HWND hWnd
// Handle for window to which notification is posted.
//
// UINT uiWM_USER
// Users assigned windows message identification
//
// DWORD dwP2PeventFilter
// P2Pevent's filter mask to be applied
//
// DWORD_PTR dwUserKey
// User defined message key.
//
static HWND g_HWnd = 0;
static UINT s_uiWM_USER = 0;
static DWORD s_dwEventFilter = 0;
static DWORD_PTR s_dwUserKey = 0;
void
P2Pevent::Register4P2Pevents ( HWND hWnd, UINT uiWM_USER
, DWORD dwEventFilter, DWORD_PTR dwUserKey )
{
g_HWnd = hWnd;
s_uiWM_USER = uiWM_USER;
s_dwEventFilter = dwEventFilter;
s_dwUserKey = dwUserKey;
}
static P2PeventCBFnc s_pfnP2PeventCB = nullptr;
static P2PeventSinkID s_nP2PeventSinkID = 0;
static DWORD s_dwSinkP2PeventFilter = 0;
static DWORD_PTR s_dwSinkUserKey = 0;
void
P2Pevent::Register4P2Pevents ( P2PeventSinkID nP2PeventSinkID, P2PeventCBFnc pfnP2PeventCB
, DWORD dwP2PeventFilter, DWORD_PTR dwSinkUserKey )
{
s_pfnP2PeventCB = pfnP2PeventCB;
s_nP2PeventSinkID = nP2PeventSinkID;
s_dwSinkP2PeventFilter = dwP2PeventFilter;
s_dwSinkUserKey = dwSinkUserKey;
}
///////////////////////////////////////////////////////////////////////
// Factories
//
// Makes empty P2Pevent image suitable for chaining
// NOTES: Problematic exercise on the edge of application stability
//
// Parameters: P2Pevent_e eClass
// Instance class
//
// Returns: P2Pevent*
// Pointer to P2Pevent object suitable P2Pevent chaining
P2Pevent*
P2Pevent::MakeEvent( P2Pevent_e eClass )
{
// Implementation
P2Pevent *pP2Pevent = 0;
try
{
pP2Pevent = new P2Pevent ( );
((P2PeventNode *)pP2Pevent->c_vBlob()) -> eClass = static_cast<BYTE>(eClass);
((P2PeventNode *)pP2Pevent->c_vBlob()) -> dwEpoch = time(0);
if ( s_szServiceName[0] )
(*pP2Pevent).DESC += P3PmsgField ( TEvent__Svc, DataWSTR08(s_szServiceName) );
// Origin of event details
// NOTES: We may or may not be P2PmsgHub context
//P2PmsgHubID nP2PmsgHubID = GetP2PmsgHubContext();
//if ( nP2PmsgHubID > 0 )
//{
// LPCWSTR lpszHubName = GetP2PmsgHubName ( nP2PmsgHubID );
// (*pP2Pevent) += P3PmsgField ( TEvent__Hub, DataBSTR08(lpszHubName) );
// LPCWSTR lpszPumpName = GetP2PmsgPumpName( );
// (*pP2Pevent) += P3PmsgField ( TEvent__Pmp, DataBSTR08(lpszPumpName) );
//}
}
catch ( ... )
{
delete pP2Pevent;
pP2Pevent = 0;
throw; // Propagate original exception
}
// Never a NULL pointer
return pP2Pevent;
}
//
// Initialise P2Pevent object
// NOTES: Problematic exercise on the edge of application stability
//
// Parameters: P2Pevent_e eClass
// Instance class
//
// Returns: P2Pevent*
// Pointer to P2Pevent object suitable P2Pevent chaining
P2Pevent*
P2Pevent::InitEvent( P2Pevent_e eClass )
{
// Implementation
((P2PeventNode *)c_vBlob()) -> eClass = static_cast<BYTE>(eClass);
((P2PeventNode *)c_vBlob()) -> dwEpoch = time(0);
if ( s_szServiceName[0] )
(*this).DESC += P3PmsgField ( TEvent__Svc, DataWSTR08(s_szServiceName) );
// Origin of event details
// NOTES: We may or may not be P2PmsgHub context
//P2PmsgHubID nP2PmsgHubID = GetP2PmsgHubContext();
//if ( nP2PmsgHubID > 0 )
//{
// LPCWSTR lpszHubName = GetP2PmsgHubName ( nP2PmsgHubID );
// (*this) += P3PmsgField ( TEvent__Hub, DataBSTR08(lpszHubName) );
// LPCWSTR lpszPumpName = GetP2PmsgPumpName( );
// (*this) += P3PmsgField ( TEvent__Pmp, DataBSTR08(lpszPumpName) );
//}
// Never a NULL pointer
return this;
}
///////////////////////////////////////////////////////////////////////
// Overloaded Operators
//
// operator = () overload
//
// Parameters: const P2Pevent& rhs
// Right hand side
//
// Returns: P2Pevent&
// Self reference
//
P2Pevent&
P2Pevent::operator = ( const P2Pevent& rhs )
{
// Optimise
if ( this == &rhs )
return *this;
P3PmsgItem::operator = ( rhs );
// Tidy up and
return *this;
}
///////////////////////////////////////////////////////////////////////
// Operations
//
// Sets formatted modulename
// NOTES: Displaces previous field contents
//
// Parameters: const char *pszModuleName
// Module name to be formatted and set
//
// ...
// Variable argument list associated with above format
// string.
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
#ifdef _UNICODE
P2Pevent*
P2Pevent::Module ( LPCWSTR lpszModuleFormat, ... )
{
// Locals
if ( lpszModuleFormat == nullptr )
return this;
va_list ap;
WCHAR szModule[512] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszModuleFormat );
if ( lpszModuleFormat )
cSize += _vstprintf_s ( &szModule[cSize], ARRAYSIZE(szModule)-cSize, lpszModuleFormat, ap );
szModule[cSize++] = 0;
VERIFY(cSize<255);
// Persist and
Module_ ( &szModule[0] );
// Tidy up, and
va_end ( ap );
return this;
}
#endif
P2Pevent*
P2Pevent::Module ( LPCSTR lpszModuleFormat, ... )
{
// Locals
if ( lpszModuleFormat == 0 )
return this;
va_list ap;
char szModule[512] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszModuleFormat );
if ( lpszModuleFormat )
cSize += vsprintf_s ( &szModule[cSize], ARRAYSIZE(szModule)-cSize, lpszModuleFormat, ap );
szModule[cSize++] = 0;
va_end ( ap );
// Persist and
#ifdef _UNICODE
CString strModule = ATL::CA2WEX(&szModule[0]).m_szBuffer;
Module_ ( strModule );
#else
Module_ ( szModule );
#endif
// Tidy up, and
return this;
}
P2Pevent*
P2Pevent::Module_ ( LPCWSTR lpszModule )
{
#ifdef _UNICODE
P3PmsgData oData = DataWSTR08(lpszModule);
#else
P3PmsgData oData = DataBSTR08(lpszModule);
#endif
// Persist and
if ( P3PmsgItem::Exists(TEvent__Fnc) )
(*this)[TEvent__Fnc] = oData;
else
(*this).DESC += P3PmsgItem ( TEvent__Fnc, oData );
// Tidy up, and
return this;
}
//
// Allocates P2Pevent message
// NOTES: Build a list of successive message strings
//
// Parameters: LPCWSTR lpszMessageFormat
// Message format
//
// ...
// Variable argument list associated with above format
// string.
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
#ifdef _UNICODE
P2Pevent*
P2Pevent::Message ( LPCWSTR lpszMessageFormat, ... )
{
// Locals
va_list ap;
WCHAR szModule[4096] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszMessageFormat );
if ( lpszMessageFormat )
cSize += _vstprintf_s ( &szModule[cSize], ARRAYSIZE(szModule)-cSize, lpszMessageFormat, ap );
szModule[cSize++] = 0;
VERIFY(cSize<255);
// Persist and
if ( cSize > 1 )
Message_ ( szModule );
// Tidy up, and
va_end ( ap );
return this;
}
#endif
P2Pevent*
P2Pevent::Message ( LPCSTR lpszMessageFormat, ... )
{
// Locals
va_list ap;
char szMessage[2048] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszMessageFormat );
if ( lpszMessageFormat )
cSize += vsprintf_s ( &szMessage[cSize], ARRAYSIZE(szMessage)-cSize, lpszMessageFormat, ap );
szMessage[cSize++] = 0;
va_end ( ap );
// Persist and
if ( cSize > 1 ) {
CString strMessage = szMessage;
Message_ ( strMessage );
}
// Tidy up, and
return this;
}
P2Pevent*
P2Pevent::Message_ ( LPCWSTR lpszMessage )
{
// Persist and
if ( !P3PmsgItem::Exists(TEvent__Dsc) )
(*this).DESC += P3PmsgList ( P3PmsgField(TEvent__Dsc) );
P3PmsgList& oList = dynamic_cast<P3PmsgList&>((*this)[TEvent__Dsc]);
if ( wcslen(lpszMessage) < 127 )
oList += DataWSTR08(lpszMessage);
else
oList += DataWSTR16(lpszMessage);
// Tidy up, and
m_csDescription.Empty ( ); // Activates reformat
return this;
}
//
// Allocates P2Pevent advice
// NOTES: Build a list of successive advice strings
//
// Parameters: LPCWSTR lpszFormat
// Advice format
//
// ...
// Variable argument list associated with above format
// string.
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
#ifdef _UNICODE
P2Pevent*
P2Pevent::Advice ( LPCWSTR lpszAdviceFormat, ... )
{
// Locals
va_list ap;
WCHAR szAdvice[512] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszAdviceFormat );
if ( lpszAdviceFormat )
cSize += _vstprintf_s ( &szAdvice[cSize], ARRAYSIZE(szAdvice)-cSize, lpszAdviceFormat, ap );
szAdvice[cSize++] = 0;
VERIFY(cSize<255);
// Persist and
Advice_ ( szAdvice );
// Tidy up, and
va_end ( ap );
return this;
}
#endif
P2Pevent*
P2Pevent::Advice ( LPCSTR lpszAdviceFormat, ... )
{
// Locals
va_list ap;
char szAdvice[512] = {0};
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszAdviceFormat );
if ( lpszAdviceFormat )
cSize += vsprintf_s ( &szAdvice[cSize], ARRAYSIZE(szAdvice)-cSize, lpszAdviceFormat, ap );
szAdvice[cSize++] = 0;
va_end ( ap );
// Persist and
#ifdef _UNICODE
CString strAdviceFormat = ATL::CA2WEX(lpszAdviceFormat).m_szBuffer;
Advice_ ( strAdviceFormat );
#else
Advice_ ( lpszAdviceFormat );
#endif
// Tidy up, and
return this;
}
P2Pevent*
P2Pevent::Advice_ ( LPCWSTR lpszAdvice )
{
// Persist and
if ( !P3PmsgItem::Exists(TEvent__Adv) )
(*this).DESC += P3PmsgList ( P3PmsgField(TEvent__Adv) );
P3PmsgList& oList = dynamic_cast<P3PmsgList&>((*this)[TEvent__Adv]);
if ( wcslen(lpszAdvice) < 127 )
oList += DataWSTR08(lpszAdvice);
else
oList += DataWSTR16(lpszAdvice);
// Tidy up, and
m_csDescription.Empty ( ); // Activates reformat
return this;
}
//
// Sets error group
// NOTES: Usually a short description
// WIN.. Windows
// WSA.. Windows Socket Architecture
// P2P.. Peer2Peer
// : Displaces previous field contents
//
//
// Parameters: LPCWSTR lpszGroup
//
// ...
// Variable argument list associated with above
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
#ifdef _UNICODE
P2Pevent*
P2Pevent::Group ( LPCWSTR lpszGroupFormat, ... )
{
// Locals
va_list ap;
WCHAR szGroup[512];
int cSize = 0;
// Complete formating from variable argument list.
va_start(ap, lpszGroupFormat);
if ( lpszGroupFormat )
cSize += _vstprintf_s (&szGroup[cSize], ARRAYSIZE(szGroup) - cSize, lpszGroupFormat, ap);
szGroup[cSize++] = 0;
VERIFY(cSize<255);
// Persist and
Group_ ( szGroup );
// Tidy up, and
va_end(ap);
return this;
}
#endif
P2Pevent*
P2Pevent::Group ( LPCSTR lpszGroupFormat, ... )
{
// Locals
va_list ap;
char szGroup[512];
int cSize = 0;
// Complete formating from variable argument list.
va_start ( ap, lpszGroupFormat );
if (lpszGroupFormat)
cSize += vsprintf_s ( &szGroup[cSize], ARRAYSIZE(szGroup) - cSize, lpszGroupFormat, ap );
szGroup[cSize++] = 0;
va_end(ap);
// Persist and
#ifdef _UNICODE
CString strGroupFormat = ATL::CA2WEX(lpszGroupFormat).m_szBuffer;
Group_(strGroupFormat);
#else
Group_(lpszGroupFormat);
#endif
// Tidy up, and
return this;
}
P2Pevent*
P2Pevent::Group_ ( LPCWSTR lpszGroup )
{
P3PmsgData oData = DataWSTR08(lpszGroup);
// Persist and
if ( P3PmsgItem::Exists(TEvent__Grp) )
(*this)[TEvent__Grp] = oData;
else
(*this).DESC += P3PmsgField ( TEvent__Grp,oData );
// Tidy up, and
return this;
}
//
// Sets HRESULT code for object
// NOTES: Over writes existing HRESULT for this level
//
//
// Parameters: HRESULT hr
// System error to be set
//
// LPCWSTR lpszhr = 0
// Text description associated with above error
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
P2Pevent*
P2Pevent::HResult ( HRESULT hr, LPCWSTR lpszHR )
{
if ( hr == 0 )
hr = GetLastError();
if ( hr == 0 )
hr = errno;
if ( hr == 0 )
hr = _doserrno;
//if ( hr == 0 )
// hr = std::stderror;
// Persist
WCHAR szHR[256] = {0};
if ( lpszHR == 0 )
{
DWORD dwSize = FormatMessage ( FORMAT_MESSAGE_FROM_SYSTEM
, 0
, hr
, 0
, szHR, ARRAYSIZE(szHR)
, 0 );
szHR[dwSize] = 0;
lpszHR = szHR;
}
// Simply
((P2PeventNode *)c_vBlob()) -> hr = hr;
if ( P3PmsgItem::Exists(TEvent__Sys) )
(*this)[TEvent__Sys] = DataWSTR16(lpszHR);
else
(*this).DESC += P3PmsgField ( TEvent__Sys, DataWSTR16(lpszHR) );
return this;
}
P2Pevent*
P2Pevent::HResultHMODULE ( HRESULT hr, LPCWSTR lpszHMODULEname, LPCWSTR lpszHR )
{
if ( hr == 0 )
hr = GetLastError();
if ( hr == 0 )
hr = errno;
if ( hr == 0 )
hr = _doserrno;
// Persist
WCHAR szHR[256] = {0};
DWORD dwSize = 0;
if ( lpszHR == nullptr )
{
dwSize = FormatMessage ( /*FORMAT_MESSAGE_ALLOCATE_BUFFER |*/ FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS
, reinterpret_cast<LPCVOID>(GetModuleHandle(lpszHMODULEname))
, hr
, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
, &szHR[0] //reinterpret_cast<LPWSTR>(&szHR)
, ARRAYSIZE(szHR), NULL);
szHR[dwSize] = 0;
}
// Delgate
if ( dwSize > 0 )
return HResult ( hr, &szHR[0] );
return HResult ( hr, 0 );
}
//
// Sets this P2Pevent as our last thread P2Pevent
// NOTES: Delegates directly through to SetP2Pevent()
//
//
// Returns: P2Pevent*
// Pointer to this object suitable for P2Pevent chaining
P2Pevent*
P2Pevent::SetLast ( )
{
// Simply
SetP2Pevent ( this );
return this;
}
// Throws pointer to this P2Pevent
// NOTES: Allocated memory must be released upon capture
// : Usually only EVERR type events are thrown
//
// Parameters: bool bSetLastEvent = true
// true... Set this event as last event for
// thread
// false.. Skip last thread event sequence
void
P2Pevent::Throw ( bool bSetLastEvent )
{
#if !defined(_WIN32)
if ( getenv("P2P_ERR_TRACE") )
fprintf(stderr, "[THROW] module='%ls' msg='%ls' advice='%ls'\n",
GetModule() ? GetModule() : L"",
(const wchar_t*)GetMessage().GetString(),
(const wchar_t*)GetAdvice().GetString());
#endif
// Thrown P2Pevent's become the last
if ( bSetLastEvent )
SetLast ( );
// Simply
throw this;
}
///////////////////////////////////////////////////////////////////////
// Deployment configuration
// NOTES: One plain text file, two settings, read once. Refer Msgexception.h
// for the format and for the two rules that make it safe.
//
// A note on what is NOT here. This deliberately does not use the registry,
// an XML/JSON parser, or MFC's CWinApp profile helpers. Everything below
// runs on the diagnostic path of a library that is loaded into hosts as
// different as an MFC application, a bare JVM and a Windows service, and
// compiles for Linux as well. A dependency taken here is a dependency taken
// everywhere, to read two lines.
//
// It also cannot report its own troubles the normal way: raising a P2Pevent
// about a malformed configuration file would re-enter Display(), which is the
// code asking for the configuration. Complaints are therefore ACCUMULATED
// into a note and emitted with the first diagnostic that goes to the log,
// which is the one place they are certain to be read.
static const size_t s_cchP2PcfgPATH = 1024;
// Declared HERE rather than beside P2PeventTextOnly() below, because
// LoadConfigFile() drives it and is defined first. CONSTANT-initialised on
// purpose: refer P2PeventTextOnly() for why a dynamic initialiser on this
// particular variable is an ordering hazard.
static int s_nP2PeventTextOnly = -1; // -1 unresolved, 0 dialog may be used, 1 text only
static bool s_bP2PcfgLoaded = false; // search performed (found or not)
static bool s_bP2PcfgTextOnly = false; // "ErrToMessageBox: 0" was read
static bool s_bP2PcfgNoted = false; // note already written to the log
static wchar_t s_szP2PcfgPath [ s_cchP2PcfgPATH ] = { 0 }; // file that was read
static wchar_t s_szP2PlogPath [ s_cchP2PcfgPATH ] = { 0 }; // resolved log, "" = stderr
static wchar_t s_szP2PcfgNote [ 1024 ] = { 0 }; // accumulated complaints
//
// The one lock, guarding the configuration statics and the log write
// NOTES: Function-local so the CRITICAL_SECTION is initialised exactly once
// by the compiler's thread-safe static, without a dynamic initialiser
// at namespace scope - the ordering hazard the text-only latch above
// is constant-initialised to avoid.
// : The section is never copied. A CRITICAL_SECTION is documented
// opaque, and its DebugInfo refers back to the object's own address,
// so returning one by value from an initialising lambda would produce
// a section pointing at a temporary that has gone.
// : Held across the fopen/fwrite/fclose of a log write, so it must be
// RECURSIVE - and it is, both as a Win32 section and as the Linux
// shim's std::recursive_mutex - because the write path may still have
// to complete the lazy load underneath itself.
static CRITICAL_SECTION s_csP2Pcfg;
static CRITICAL_SECTION*
P2PcfgLock ( )
{
static const int s_nOnce = [] () -> int
{
::InitializeCriticalSection ( &s_csP2Pcfg );
return 1;
} ();
(void)s_nOnce;
return &s_csP2Pcfg;
}
//
// Records a complaint about the configuration, for later emission
//
// Parameters: LPCWSTR lpszWhat
// One clause, no trailing punctuation.
static void
P2PcfgNote ( LPCWSTR lpszWhat )
{
if ( !lpszWhat || !*lpszWhat )
return;
size_t nUsed = ::wcslen ( s_szP2PcfgNote );
size_t nRoom = ( sizeof(s_szP2PcfgNote) / sizeof(wchar_t) ) - 1;
if ( nUsed && nUsed + 2 < nRoom )
{
s_szP2PcfgNote [ nUsed++ ] = L';';
s_szP2PcfgNote [ nUsed++ ] = L' ';
}
for ( size_t i = 0; lpszWhat[i] && nUsed < nRoom; i++ )
s_szP2PcfgNote [ nUsed++ ] = lpszWhat[i];
s_szP2PcfgNote [ nUsed ] = 0;
}
//
// Copies a wide string into a fixed buffer, truncating rather than failing
// NOTES: Not wcsncpy_s. The Linux shim carries wcscpy_s and not the counted
// form, and a diagnostic path is the last place to want a build that
// only compiles on one of the two platforms.
static void
P2PcfgCopy ( wchar_t *pszTo, size_t cchTo, LPCWSTR lpszFrom )
{
if ( !pszTo || !cchTo )
return;
size_t i = 0;
if ( lpszFrom )
for ( ; lpszFrom[i] && i + 1 < cchTo; i++ )
pszTo[i] = lpszFrom[i];
pszTo[i] = 0;
}
//
// Opens a file by wide path, on either platform
// NOTES: There is no _wfopen on Linux and the shim does not invent one, so
// the path is converted to UTF-8 there. On Windows the wide call is
// kept, so a path outside the active code page still opens.
static FILE*
P2PcfgOpen ( LPCWSTR lpszPath, const char *pszMode )
{
if ( !lpszPath || !*lpszPath || !pszMode )
return 0;
#if defined(_WIN32)
wchar_t wszMode [ 8 ] = { 0 };
for ( int i = 0; pszMode[i] && i < 7; i++ )
wszMode[i] = (wchar_t)(unsigned char)pszMode[i];
FILE *pf = 0;
if ( ::_wfopen_s ( &pf, lpszPath, wszMode ) != 0 )
return 0;
return pf;
#else
char szPath [ s_cchP2PcfgPATH * 4 ] = { 0 };
if ( ::WideCharToMultiByte ( CP_UTF8, 0, lpszPath, -1
, szPath, (int)sizeof(szPath), 0, 0 ) <= 0 )
return 0;
return ::fopen ( szPath, pszMode );
#endif
}
//
// Directory part of a path, WITHOUT the trailing separator
// NOTES: Hand-rolled rather than _wsplitpath_s, which would need four
// buffers to discard three of them, and accepts either separator
// because a configuration path may arrive from anywhere.
static void
P2PcfgDirOf ( LPCWSTR lpszPath, wchar_t *pszDir, size_t cchDir )
{
P2PcfgCopy ( pszDir, cchDir, lpszPath );
for ( size_t i = ::wcslen ( pszDir ); i > 0; i-- )
if ( pszDir[i-1] == L'\\' || pszDir[i-1] == L'/' )
{
pszDir[i-1] = 0;
return;
}
pszDir[0] = 0; // no separator: no directory part
}
//
// Is this path already absolute?
static bool
P2PcfgIsAbsolute ( LPCWSTR lpszPath )
{
if ( !lpszPath || !*lpszPath )
return false;
if ( lpszPath[0] == L'\\' || lpszPath[0] == L'/' )
return true; // rooted, or a UNC share
return ( lpszPath[1] == L':' ); // drive-qualified
}
//
// Resolves a possibly relative name against a directory
// NOTES: THE DIRECTORY IS THE CONFIGURATION FILE'S, never the working
// directory. A service started by the SCM inherits
// %SystemRoot%\System32 as its working directory, so a bare
// "errorLog.txt" resolved against the CWD would try to write into a
// system folder - which fails unelevated, and is vandalism when it
// succeeds. Resolving against the file that named it puts the log
// where whoever wrote the configuration was looking.
static void
P2PcfgResolve ( LPCWSTR lpszDir, LPCWSTR lpszName
, wchar_t *pszOut, size_t cchOut )
{
if ( P2PcfgIsAbsolute ( lpszName ) || !lpszDir || !*lpszDir )
{
P2PcfgCopy ( pszOut, cchOut, lpszName );
return;
}
P2PcfgCopy ( pszOut, cchOut, lpszDir );
size_t n = ::wcslen ( pszOut );
if ( n + 1 < cchOut )
pszOut[n++] = L'\\';
pszOut[n] = 0;
for ( size_t i = 0; lpszName[i] && n + 1 < cchOut; i++ )
pszOut[n++] = lpszName[i];
pszOut[n] = 0;
}
//
// Reads a boolean setting
// NOTES: The three spellings the setting documents, plus true/false and
// enable/disable, because somebody will write them and refusing a
// word whose meaning is unambiguous serves nobody.
// : An UNRECOGNISED value leaves the setting alone and complains. It
// must not fall to either default silently: reading "ErrToMessageBox:
// maybe" as OFF would suppress a windowed application's dialogs on
// the strength of a typo, and reading it as ON would suppress the
// evidence that the line was never understood.
//
// Returns: bool
// true ... *pbValue was set
static bool
P2PcfgParseBool ( LPCWSTR lpszValue, bool *pbValue )
{
static LPCWSTR s_apszTrue [] = { L"1", L"on", L"yes", L"true"
, L"enable", L"enabled" };
static LPCWSTR s_apszFalse[] = { L"0", L"off", L"no", L"false"
, L"disable", L"disabled" };
for ( size_t i = 0; i < sizeof(s_apszTrue)/sizeof(s_apszTrue[0]); i++ )
if ( ::_wcsicmp ( lpszValue, s_apszTrue[i] ) == 0 )
{ *pbValue = true; return true; }
for ( size_t i = 0; i < sizeof(s_apszFalse)/sizeof(s_apszFalse[0]); i++ )
if ( ::_wcsicmp ( lpszValue, s_apszFalse[i] ) == 0 )
{ *pbValue = false; return true; }
return false;
}
//
// Trims ASCII blanks from both ends of a wide buffer, in place
static wchar_t*
P2PcfgTrim ( wchar_t *psz )
{
while ( *psz == L' ' || *psz == L'\t' )
psz++;
size_t n = ::wcslen ( psz );
while ( n && ( psz[n-1] == L' ' || psz[n-1] == L'\t'
|| psz[n-1] == L'\r' || psz[n-1] == L'\n' ) )
psz[--n] = 0;
return psz;
}
//
// Applies one "Key: Value" line
// NOTES: '=' is accepted as well as ':' because a caller who has written one
// .ini file in their life will type it, and the intent is not in
// doubt.
static void
P2PcfgApplyLine ( wchar_t *pszLine, LPCWSTR lpszDir
, bool *pbLogNamed )
{
wchar_t *pszKey = P2PcfgTrim ( pszLine );
if ( !*pszKey || *pszKey == L'#' || *pszKey == L';' )
return; // blank, or a comment
if ( pszKey[0] == L'/' && pszKey[1] == L'/' )
return;
wchar_t *pszSep = pszKey;
while ( *pszSep && *pszSep != L':' && *pszSep != L'=' )
pszSep++;
if ( !*pszSep )
{
P2PcfgNote ( L"line without a ':' ignored" );
return;
}
*pszSep = 0;
wchar_t *pszValue = P2PcfgTrim ( pszSep + 1 );
pszKey = P2PcfgTrim ( pszKey );
if ( ::_wcsicmp ( pszKey, P2PMSG_CFG_DIALOG ) == 0 )
{
bool bDialog = true;
if ( !P2PcfgParseBool ( pszValue, &bDialog ) )
P2PcfgNote ( L"ErrToMessageBox value not understood, setting ignored" );
else if ( !bDialog )
s_bP2PcfgTextOnly = true; // one way only: refer LoadConfigFile
}
else if ( ::_wcsicmp ( pszKey, P2PMSG_CFG_LOG ) == 0 )
{
if ( !*pszValue )
P2PcfgNote ( L"LogFile named nothing, setting ignored" );
else
{
P2PcfgResolve ( lpszDir, pszValue, s_szP2PlogPath, s_cchP2PcfgPATH );
*pbLogNamed = true;
}
}
else
P2PcfgNote ( L"unknown setting ignored" );
}
//
// Reads and applies one configuration file