Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1148)

Side by Side Diff: base/logging.cc

Issue 244583002: Extracted logging::SystemErrorCodeToString function. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/logging.h ('k') | base/logging_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/logging.h" 5 #include "base/logging.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #include <windows.h> 9 #include <windows.h>
10 typedef HANDLE FileHandle; 10 typedef HANDLE FileHandle;
(...skipping 25 matching lines...) Expand all
36 #define MAX_PATH PATH_MAX 36 #define MAX_PATH PATH_MAX
37 typedef FILE* FileHandle; 37 typedef FILE* FileHandle;
38 typedef pthread_mutex_t* MutexHandle; 38 typedef pthread_mutex_t* MutexHandle;
39 #endif 39 #endif
40 40
41 #include <algorithm> 41 #include <algorithm>
42 #include <cstring> 42 #include <cstring>
43 #include <ctime> 43 #include <ctime>
44 #include <iomanip> 44 #include <iomanip>
45 #include <ostream> 45 #include <ostream>
46 #include <string>
46 47
47 #include "base/base_switches.h" 48 #include "base/base_switches.h"
48 #include "base/command_line.h" 49 #include "base/command_line.h"
49 #include "base/debug/alias.h" 50 #include "base/debug/alias.h"
50 #include "base/debug/debugger.h" 51 #include "base/debug/debugger.h"
51 #include "base/debug/stack_trace.h" 52 #include "base/debug/stack_trace.h"
52 #include "base/posix/eintr_wrapper.h" 53 #include "base/posix/eintr_wrapper.h"
53 #include "base/strings/string_piece.h" 54 #include "base/strings/string_piece.h"
55 #include "base/strings/string_util.h"
56 #include "base/strings/stringprintf.h"
54 #include "base/strings/utf_string_conversions.h" 57 #include "base/strings/utf_string_conversions.h"
55 #include "base/synchronization/lock_impl.h" 58 #include "base/synchronization/lock_impl.h"
56 #include "base/threading/platform_thread.h" 59 #include "base/threading/platform_thread.h"
57 #include "base/vlog.h" 60 #include "base/vlog.h"
58 #if defined(OS_POSIX) 61 #if defined(OS_POSIX)
59 #include "base/safe_strerror_posix.h" 62 #include "base/safe_strerror_posix.h"
60 #endif 63 #endif
61 64
62 #if defined(OS_ANDROID) 65 #if defined(OS_ANDROID)
63 #include <android/log.h> 66 #include <android/log.h>
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 #if defined(OS_WIN) 725 #if defined(OS_WIN)
723 return ::GetLastError(); 726 return ::GetLastError();
724 #elif defined(OS_POSIX) 727 #elif defined(OS_POSIX)
725 return errno; 728 return errno;
726 #else 729 #else
727 #error Not implemented 730 #error Not implemented
728 #endif 731 #endif
729 } 732 }
730 733
731 #if defined(OS_WIN) 734 #if defined(OS_WIN)
735 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
736 const int error_message_buffer_size = 256;
737 char msgbuf[error_message_buffer_size];
738 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
739 DWORD len = FormatMessageA(flags, NULL, error_code, 0, msgbuf,
740 arraysize(msgbuf), NULL);
741 if (len) {
742 // Messages returned by system end with line breaks.
743 return base::CollapseWhitespaceASCII(msgbuf, true) +
744 base::StringPrintf(" (0x%X)", error_code);
745 }
746 return base::StringPrintf("Error (0x%X) while retrieving error. (0x%X)",
747 GetLastError(), error_code);
748 }
749 #elif defined(OS_POSIX)
750 BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
751 return safe_strerror(error_code);
752 }
753 #else
754 #error Not implemented
755 #endif
756
757
758 #if defined(OS_WIN)
732 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, 759 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
733 int line, 760 int line,
734 LogSeverity severity, 761 LogSeverity severity,
735 SystemErrorCode err,
736 const char* module)
737 : err_(err),
738 module_(module),
739 log_message_(file, line, severity) {
740 }
741
742 Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
743 int line,
744 LogSeverity severity,
745 SystemErrorCode err) 762 SystemErrorCode err)
746 : err_(err), 763 : err_(err),
747 module_(NULL),
748 log_message_(file, line, severity) { 764 log_message_(file, line, severity) {
749 } 765 }
750 766
751 Win32ErrorLogMessage::~Win32ErrorLogMessage() { 767 Win32ErrorLogMessage::~Win32ErrorLogMessage() {
752 const int error_message_buffer_size = 256; 768 stream() << ": " << SystemErrorCodeToString(err_);
753 char msgbuf[error_message_buffer_size];
754 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
755 HMODULE hmod;
756 if (module_) {
757 hmod = GetModuleHandleA(module_);
758 if (hmod) {
759 flags |= FORMAT_MESSAGE_FROM_HMODULE;
760 } else {
761 // This makes a nested Win32ErrorLogMessage. It will have module_ of NULL
762 // so it will not call GetModuleHandle, so recursive errors are
763 // impossible.
764 DPLOG(WARNING) << "Couldn't open module " << module_
765 << " for error message query";
766 }
767 } else {
768 hmod = NULL;
769 }
770 DWORD len = FormatMessageA(flags,
771 hmod,
772 err_,
773 0,
774 msgbuf,
775 sizeof(msgbuf) / sizeof(msgbuf[0]),
776 NULL);
777 if (len) {
778 while ((len > 0) &&
779 isspace(static_cast<unsigned char>(msgbuf[len - 1]))) {
780 msgbuf[--len] = 0;
781 }
782 stream() << ": " << msgbuf;
783 } else {
784 stream() << ": Error " << GetLastError() << " while retrieving error "
785 << err_;
786 }
787 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a 769 // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a
788 // field) and use Alias in hopes that it makes it into crash dumps. 770 // field) and use Alias in hopes that it makes it into crash dumps.
789 DWORD last_error = err_; 771 DWORD last_error = err_;
790 base::debug::Alias(&last_error); 772 base::debug::Alias(&last_error);
791 } 773 }
792 #elif defined(OS_POSIX) 774 #elif defined(OS_POSIX)
793 ErrnoLogMessage::ErrnoLogMessage(const char* file, 775 ErrnoLogMessage::ErrnoLogMessage(const char* file,
794 int line, 776 int line,
795 LogSeverity severity, 777 LogSeverity severity,
796 SystemErrorCode err) 778 SystemErrorCode err)
797 : err_(err), 779 : err_(err),
798 log_message_(file, line, severity) { 780 log_message_(file, line, severity) {
799 } 781 }
800 782
801 ErrnoLogMessage::~ErrnoLogMessage() { 783 ErrnoLogMessage::~ErrnoLogMessage() {
802 stream() << ": " << safe_strerror(err_); 784 stream() << ": " << SystemErrorCodeToString(err_);
803 } 785 }
804 #endif // OS_WIN 786 #endif // OS_WIN
805 787
806 void CloseLogFile() { 788 void CloseLogFile() {
807 LoggingLock logging_lock; 789 LoggingLock logging_lock;
808 CloseLogFileUnlocked(); 790 CloseLogFileUnlocked();
809 } 791 }
810 792
811 void RawLog(int level, const char* message) { 793 void RawLog(int level, const char* message) {
812 if (level >= min_log_level) { 794 if (level >= min_log_level) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 return *log_file_name; 830 return *log_file_name;
849 return std::wstring(); 831 return std::wstring();
850 } 832 }
851 #endif 833 #endif
852 834
853 } // namespace logging 835 } // namespace logging
854 836
855 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { 837 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) {
856 return out << base::WideToUTF8(std::wstring(wstr)); 838 return out << base::WideToUTF8(std::wstring(wstr));
857 } 839 }
OLDNEW
« no previous file with comments | « base/logging.h ('k') | base/logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698