Revision | eb306a549bca74079adb9a7d6f4c9d2023ec3777 (tree) |
---|---|
Time | 2011-10-12 01:08:09 |
Author | Kazuhiro Fujieda <fujieda@user...> |
Commiter | Kazuhiro Fujieda |
Rewrite CUtils::Log with _vstprintf_s.
@@ -574,85 +574,34 @@ BOOL CUtils::IsBash() | ||
574 | 574 | return !_tcsicmp(m_szApplicationName, _T("bash.exe")); |
575 | 575 | } |
576 | 576 | |
577 | +static void invalid_parameter_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t) | |
578 | +{ | |
579 | + return; | |
580 | +} | |
581 | + | |
577 | 582 | // for debug |
578 | -void CUtils::Log(LPTSTR fmt, ...) | |
583 | +void CUtils::Log(LPCTSTR fmt, ...) | |
579 | 584 | { |
580 | 585 | va_list ap; |
581 | 586 | va_start(ap, fmt); |
582 | - | |
583 | - static const int LOG_MAX = 0x10000; | |
584 | - TCHAR szLog[LOG_MAX] = {'\0'}; | |
585 | - | |
586 | - for (unsigned int nIndex = 0; nIndex < _tcslen(fmt); ) { | |
587 | - LPTSTR pNextString = fmt + nIndex; | |
588 | - int len = _tcslen(szLog); | |
589 | - LPTSTR pLogEnd = szLog + len; | |
590 | - | |
591 | - if (*pNextString == _T('%')) { | |
592 | - TCHAR szFormatTag[LOG_MAX] = {'0'}; | |
593 | - _tcscpy_s(szFormatTag, pNextString); | |
594 | - | |
595 | - switch (GetFormatTag(szFormatTag)) { | |
596 | - case _T('d'): | |
597 | - case _T('i'): | |
598 | - case _T('o'): | |
599 | - case _T('x'): | |
600 | - case _T('X'): | |
601 | - case _T('u'): | |
602 | - case _T('c'): | |
603 | - _stprintf_s(pLogEnd, LOG_MAX - len, szFormatTag, va_arg(ap, int)); | |
604 | - break; | |
605 | - case _T('s'): | |
606 | - _stprintf_s(pLogEnd, LOG_MAX - len, szFormatTag, va_arg(ap, LPTSTR)); | |
607 | - break; | |
608 | - case _T('f'): | |
609 | - case _T('e'): | |
610 | - case _T('E'): | |
611 | - case _T('g'): | |
612 | - case _T('G'): | |
613 | - _stprintf_s(pLogEnd, LOG_MAX - len, szFormatTag, va_arg(ap, double)); | |
614 | - break; | |
615 | - case _T('p'): | |
616 | - _stprintf_s(pLogEnd, LOG_MAX - len, szFormatTag, va_arg(ap, void *)); | |
617 | - break; | |
618 | - case _T('n'): | |
619 | - _stprintf_s(pLogEnd, LOG_MAX - len, szFormatTag, va_arg(ap, int *)); | |
620 | - break; | |
621 | - case _T('%'): | |
622 | - default: | |
623 | - _stprintf_s(pLogEnd, LOG_MAX - len, _T("%s"), szFormatTag); | |
624 | - break; | |
625 | - } | |
626 | - | |
627 | - nIndex += _tcslen(szFormatTag); | |
628 | - } else { | |
629 | - TCHAR szString[LOG_MAX] = {'0'}; | |
630 | - _tcscpy_s(szString, pNextString); | |
631 | - LPTSTR c; | |
632 | - LPTSTR pString = _tcstok_s(szString, _T("%"), &c); | |
633 | - _stprintf_s(pLogEnd, LOG_MAX - len, _T("%s"), pString); | |
634 | - | |
635 | - nIndex += _tcslen(pString); | |
636 | - } | |
637 | - } | |
638 | - | |
639 | - va_end(ap); | |
587 | + TCHAR log[1024]; | |
588 | + _set_invalid_parameter_handler(invalid_parameter_handler); | |
589 | + if (_vstprintf_s(log, fmt, ap) < 0) | |
590 | + _tcscpy_s(log, _T("invalid format")); | |
640 | 591 | |
641 | 592 | static int n = 0; |
642 | - TCHAR szPath[MAX_PATH] = {'\0'}; | |
643 | - if (GetTempPath(MAX_PATH, szPath)) { | |
593 | + TCHAR path[MAX_PATH]; | |
594 | + if (GetTempPath(MAX_PATH, path)) { | |
644 | 595 | #ifndef _WIN64 |
645 | - _tmakepath_s(szPath, NULL, szPath, _T("xkeylog"), _T("txt")); | |
596 | + _tmakepath_s(path, NULL, path, _T("xkeylog"), _T("txt")); | |
646 | 597 | #else |
647 | - _tmakepath_s(szPath, NULL, szPath, _T("xkeylog64"), _T("txt")); | |
598 | + _tmakepath_s(path, NULL, path, _T("xkeylog64"), _T("txt")); | |
648 | 599 | #endif |
649 | - } else { | |
650 | - _tcscpy_s(szPath, _T("c:\\xkeylog.txt")); | |
651 | - } | |
600 | + } else | |
601 | + _tcscpy_s(path, _T("c:\\xkeylog.txt")); | |
652 | 602 | FILE *fp; |
653 | - _tfopen_s(&fp, szPath, _T("a")); | |
654 | - _ftprintf(fp, _T("%8d: %s %s\n"), n++, m_szApplicationName, szLog); | |
655 | - fflush(fp); | |
603 | + _tfopen_s(&fp, path, _T("a")); | |
604 | + _ftprintf(fp, _T("%8d: %s\t%s\n"), n++, m_szApplicationName, log); | |
656 | 605 | fclose(fp); |
657 | 606 | } |
658 | 607 |
@@ -738,79 +687,6 @@ BOOL CUtils::IsDialog() | ||
738 | 687 | return GetParent(GetForegroundWindow()) != NULL; |
739 | 688 | } |
740 | 689 | |
741 | -int CUtils::GetFormatTag(LPTSTR szFormatTag) | |
742 | -{ | |
743 | - if (*(szFormatTag) != _T('%')) { | |
744 | - return NULL; | |
745 | - } | |
746 | - | |
747 | - unsigned int nIndex = 1; | |
748 | - | |
749 | - // flags | |
750 | - while (nIndex < _tcslen(szFormatTag)) { | |
751 | - switch (*(szFormatTag + nIndex)) { | |
752 | - case _T('-'): | |
753 | - case _T('+'): | |
754 | - case _T(' '): | |
755 | - case _T('0'): | |
756 | - case _T('#'): | |
757 | - ++nIndex; | |
758 | - continue; | |
759 | - default: | |
760 | - break; | |
761 | - } | |
762 | - | |
763 | - break; | |
764 | - } | |
765 | - | |
766 | - // width | |
767 | - while (_istdigit(*(szFormatTag + nIndex))) { | |
768 | - ++nIndex; | |
769 | - } | |
770 | - | |
771 | - // precision | |
772 | - if (*(szFormatTag + nIndex) == _T('.')) { | |
773 | - ++nIndex; | |
774 | - while (_istdigit(*(szFormatTag + nIndex))) { | |
775 | - ++nIndex; | |
776 | - } | |
777 | - } | |
778 | - | |
779 | - // prefix | |
780 | - switch (*(szFormatTag + nIndex)) { | |
781 | - case _T('h'): | |
782 | - case _T('l'): | |
783 | - case _T('L'): | |
784 | - ++nIndex; | |
785 | - break; | |
786 | - } | |
787 | - | |
788 | - // type | |
789 | - switch (*(szFormatTag + nIndex)) { | |
790 | - case _T('d'): | |
791 | - case _T('i'): | |
792 | - case _T('o'): | |
793 | - case _T('x'): | |
794 | - case _T('X'): | |
795 | - case _T('u'): | |
796 | - case _T('c'): | |
797 | - case _T('s'): | |
798 | - case _T('f'): | |
799 | - case _T('e'): | |
800 | - case _T('E'): | |
801 | - case _T('g'): | |
802 | - case _T('G'): | |
803 | - case _T('p'): | |
804 | - case _T('n'): | |
805 | - case _T('%'): | |
806 | - *(szFormatTag + nIndex + 1) = NULL; | |
807 | - return *(szFormatTag + nIndex); | |
808 | - default: | |
809 | - *(szFormatTag + nIndex) = NULL; | |
810 | - return NULL; | |
811 | - } | |
812 | -} | |
813 | - | |
814 | 690 | BOOL CUtils::IsEudora() |
815 | 691 | { |
816 | 692 | return !_tcsicmp(m_szApplicationName, _T("Eudora.exe")); |
@@ -64,7 +64,7 @@ public: | ||
64 | 64 | static BOOL IsSh(); |
65 | 65 | static BOOL IsBash(); |
66 | 66 | static void SetCorrectApplicationName(LPTSTR szApplicationName, LPTSTR szWindowText); |
67 | - static void Log(LPTSTR fmt, ...); | |
67 | + static void Log(LPCTSTR fmt, ...); | |
68 | 68 | static BOOL IsSleipnir(); |
69 | 69 | static BOOL IsConsole(); |
70 | 70 | static BOOL IsPaint(); |
@@ -117,7 +117,6 @@ public: | ||
117 | 117 | |
118 | 118 | private: |
119 | 119 | static BOOL IsTOForEOF(); |
120 | - static int GetFormatTag(LPTSTR szFormatTag); | |
121 | 120 | static void FairConsoleApplicationName(LPTSTR szApplicationName, LPTSTR szWindowText); |
122 | 121 | static BOOL IsConsole(LPCTSTR szApplicationName); |
123 | 122 | static BOOL IsJavaW(LPCTSTR szApplicationName); |