Add glog style logging to symupload

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@658 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
nealsid@gmail.com 2010-08-19 21:53:35 +00:00
parent 39edd96373
commit e167e9e61f
4 changed files with 228 additions and 9 deletions

View File

@ -35,9 +35,15 @@
#include <fstream> #include <fstream>
#include "common/windows/string_utils-inl.h" #include "common/windows/string_utils-inl.h"
#include "common/windows/wchar_logging.h"
#include "common/windows/http_upload.h" #include "common/windows/http_upload.h"
// See comment in symupload.cc about #undef ERROR. Unfortunately this has to
// violate style guide and come after http_upload.h.
#undef ERROR
#include "third_party/glog/glog/src/windows/glog/logging.h"
namespace google_breakpad { namespace google_breakpad {
using std::ifstream; using std::ifstream;
@ -45,6 +51,28 @@ using std::ios;
static const wchar_t kUserAgent[] = L"Breakpad/1.0 (Windows)"; static const wchar_t kUserAgent[] = L"Breakpad/1.0 (Windows)";
wchar_t lastErrorMessageBuffer[1024];
// Helper method to convert Last Error into a text message. Uses a static
// buffer, so don't save the message across Win32 calls that might change
// the last error.
//
// This function saves/restores the last error and it isn't thread safe.
const wchar_t* FormatLastError() {
DWORD oldLastError = GetLastError();
wchar_t lastErrorTempBuffer[1024];
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
lastErrorTempBuffer, 1024, NULL) == 0) {
wsprintf(lastErrorMessageBuffer, L"%d: (format message failed: %d)",
oldLastError, GetLastError());
} else {
wsprintf(lastErrorMessageBuffer, L"%d: %s", oldLastError,
lastErrorTempBuffer);
}
SetLastError(oldLastError);
return lastErrorMessageBuffer;
}
// Helper class which closes an internet handle when it goes away // Helper class which closes an internet handle when it goes away
class HTTPUpload::AutoInternetHandle { class HTTPUpload::AutoInternetHandle {
public: public:
@ -72,7 +100,19 @@ bool HTTPUpload::SendRequest(const wstring &url,
if (response_code) { if (response_code) {
*response_code = 0; *response_code = 0;
} }
VLOG(1) << "HTTPUpload::SendRequest";
VLOG(1) << "\tURL: " << url;
VLOG(1) << "\tUpload_File: " << upload_file;
VLOG(1) << "\tFile_part_name: " << file_part_name;
VLOG(1) << "\tParameters: ";
string s;
if (VLOG_IS_ON(1)) {
for (std::map<wstring, wstring>::const_iterator it = parameters.begin();
it != parameters.end(); ++it) {
VLOG(1) << "\t\t" << it->first << " = " << it->second;
}
}
// TODO(bryner): support non-ASCII parameter names // TODO(bryner): support non-ASCII parameter names
if (!CheckParameters(parameters)) { if (!CheckParameters(parameters)) {
return false; return false;
@ -90,7 +130,9 @@ bool HTTPUpload::SendRequest(const wstring &url,
components.lpszUrlPath = path; components.lpszUrlPath = path;
components.dwUrlPathLength = sizeof(path) / sizeof(path[0]); components.dwUrlPathLength = sizeof(path) / sizeof(path[0]);
if (!InternetCrackUrl(url.c_str(), static_cast<DWORD>(url.size()), if (!InternetCrackUrl(url.c_str(), static_cast<DWORD>(url.size()),
0, &components)) { 0, &components)) {
LOG(ERROR) << "InternetCrackUrl failed: " << FormatLastError();
LOG(ERROR) << "This indicates a malformed upload server name";
return false; return false;
} }
bool secure = false; bool secure = false;
@ -106,6 +148,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
NULL, // proxy bypass NULL, // proxy bypass
0)); // flags 0)); // flags
if (!internet.get()) { if (!internet.get()) {
LOG(ERROR) << "InternetOpen returned NULL: " << FormatLastError();
return false; return false;
} }
@ -118,6 +161,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
0, // flags 0, // flags
NULL)); // context NULL)); // context
if (!connection.get()) { if (!connection.get()) {
LOG(ERROR) << "InternetConnect returned NULL: " << FormatLastError();
return false; return false;
} }
@ -131,6 +175,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
http_open_flags, http_open_flags,
NULL)); // context NULL)); // context
if (!request.get()) { if (!request.get()) {
LOG(ERROR) << "HttpOpenRequest returned NULL: " << FormatLastError();
return false; return false;
} }
@ -152,13 +197,17 @@ bool HTTPUpload::SendRequest(const wstring &url,
INTERNET_OPTION_SEND_TIMEOUT, INTERNET_OPTION_SEND_TIMEOUT,
timeout, timeout,
sizeof(timeout))) { sizeof(timeout))) {
fwprintf(stderr, L"Could not unset send timeout, continuing...\n"); LOG(ERROR) << "InternetSetOption on send timeout returned NULL: "
<< FormatLastError();
fwprintf(stderr, L"Could not unset send timeout, continuing...\n");
} }
if (!InternetSetOption(request.get(), if (!InternetSetOption(request.get(),
INTERNET_OPTION_RECEIVE_TIMEOUT, INTERNET_OPTION_RECEIVE_TIMEOUT,
timeout, timeout,
sizeof(timeout))) { sizeof(timeout))) {
LOG(ERROR) << "InternetSetOption on receive timeout returned NULL: "
<< FormatLastError();
fwprintf(stderr, L"Could not unset receive timeout, continuing...\n"); fwprintf(stderr, L"Could not unset receive timeout, continuing...\n");
} }
} }
@ -166,6 +215,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
if (!HttpSendRequest(request.get(), NULL, 0, if (!HttpSendRequest(request.get(), NULL, 0,
const_cast<char *>(request_body.data()), const_cast<char *>(request_body.data()),
static_cast<DWORD>(request_body.size()))) { static_cast<DWORD>(request_body.size()))) {
LOG(ERROR) << "HttpSendRequest on send returned NULL: " << FormatLastError();
return false; return false;
} }
@ -175,6 +225,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
if (!HttpQueryInfo(request.get(), HTTP_QUERY_STATUS_CODE, if (!HttpQueryInfo(request.get(), HTTP_QUERY_STATUS_CODE,
static_cast<LPVOID>(&http_status), &http_status_size, static_cast<LPVOID>(&http_status), &http_status_size,
0)) { 0)) {
LOG(ERROR) << "HttpQueryInfo after send returned NULL: " << FormatLastError();
return false; return false;
} }
@ -187,6 +238,8 @@ bool HTTPUpload::SendRequest(const wstring &url,
if (result) { if (result) {
result = ReadResponse(request.get(), response_body); result = ReadResponse(request.get(), response_body);
} else {
LOG(ERROR) << "Http send request returned: " << result;
} }
return result; return result;
@ -397,11 +450,13 @@ bool HTTPUpload::CheckParameters(const map<wstring, wstring> &parameters) {
pos != parameters.end(); ++pos) { pos != parameters.end(); ++pos) {
const wstring &str = pos->first; const wstring &str = pos->first;
if (str.size() == 0) { if (str.size() == 0) {
LOG(ERROR) << "Parameter " << str << " had non ascii characters";
return false; // disallow empty parameter names return false; // disallow empty parameter names
} }
for (unsigned int i = 0; i < str.size(); ++i) { for (unsigned int i = 0; i < str.size(); ++i) {
wchar_t c = str[i]; wchar_t c = str[i];
if (c < 32 || c == '"' || c > 127) { if (c < 32 || c == '"' || c > 127) {
LOG(ERROR) << "Parameter " << str << " had non ascii characters";
return false; return false;
} }
} }

View File

@ -0,0 +1,59 @@
// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This header file defines << for wchar, which is necessary for google glog
// to correctly log strings that have that constituent type.
// See http://code.google.com/p/google-glog/issues/detail?id=4
#ifndef WCHAR_LOGGING_H_
#define WCHAR_LOGGING_H_
#include <wchar.h>
#include <iostream>
#include <string>
inline std::ostream& operator<<(std::ostream& out, const wchar_t* str) {
size_t len;
wcstombs_s(&len, NULL, 0, str, _TRUNCATE);
char* buf = (char*)malloc(len + 1);
buf[len] = 0;
size_t converted;
wcstombs_s(&converted, buf, len, str, _TRUNCATE);
out << buf;
free(buf);
return out;
}
inline std::ostream& operator<<(std::ostream& out, const std::wstring& str) {
return operator<<(out, str.c_str());
}
#endif // WCHAR_LOGGING_H_

View File

@ -51,10 +51,14 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/windows/string_utils-inl.h"
#include "common/windows/http_upload.h" #include "common/windows/http_upload.h"
#include "common/windows/pdb_source_line_writer.h" #include "common/windows/pdb_source_line_writer.h"
#include "common/windows/string_utils-inl.h"
#include "common/windows/wchar_logging.h"
// See http://code.google.com/p/google-glog/issues/detail?id=33 for why
// this #undef is added.
#undef ERROR
#include "third_party/glog/glog/src/windows/glog/logging.h"
using std::string; using std::string;
using std::wstring; using std::wstring;
@ -112,12 +116,15 @@ static bool DumpSymbolsToTempFile(const wchar_t *file,
wstring *temp_file_path, wstring *temp_file_path,
PDBModuleInfo *pdb_info) { PDBModuleInfo *pdb_info) {
google_breakpad::PDBSourceLineWriter writer; google_breakpad::PDBSourceLineWriter writer;
VLOG(1) << "DumpSymbolsToTempFile opening file";
// Use EXE_FILE to get information out of the exe/dll in addition to the // Use EXE_FILE to get information out of the exe/dll in addition to the
// pdb. The name and version number of the exe/dll are of value, and // pdb. The name and version number of the exe/dll are of value, and
// there's no way to locate an exe/dll given a pdb. // there's no way to locate an exe/dll given a pdb.
if (!writer.Open(file, PDBSourceLineWriter::EXE_FILE)) { if (!writer.Open(file, PDBSourceLineWriter::EXE_FILE)) {
VLOG(1) << "Error opening input module";
return false; return false;
} }
VLOG(1) << "DumpSymbolsToTempFile file opened";
wchar_t temp_path[_MAX_PATH]; wchar_t temp_path[_MAX_PATH];
if (GetTempPath(_MAX_PATH, temp_path) == 0) { if (GetTempPath(_MAX_PATH, temp_path) == 0) {
@ -128,6 +135,7 @@ static bool DumpSymbolsToTempFile(const wchar_t *file,
if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) { if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) {
return false; return false;
} }
VLOG(1) << "Temporary symbol filename is: " << temp_filename;
FILE *temp_file = NULL; FILE *temp_file = NULL;
#if _MSC_VER >= 1400 // MSVC 2005/8 #if _MSC_VER >= 1400 // MSVC 2005/8
@ -145,7 +153,8 @@ static bool DumpSymbolsToTempFile(const wchar_t *file,
fclose(temp_file); fclose(temp_file);
if (!success) { if (!success) {
_wunlink(temp_filename); _wunlink(temp_filename);
return false; LOG(ERROR) << "Error writing to temp file";
return false;
} }
*temp_file_path = temp_filename; *temp_file_path = temp_filename;
@ -160,11 +169,24 @@ void printUsageAndExit() {
exit(0); exit(0);
} }
int wmain(int argc, wchar_t *argv[]) { int wmain(int argc, wchar_t *argv[]) {
char progname[MAX_PATH];
size_t convertedChars;
wcstombs_s(&convertedChars, progname, MAX_PATH, argv[0], _TRUNCATE);
google::InitGoogleLogging(progname);
if ((argc != 3) && if ((argc != 3) &&
(argc != 5)) { (argc != 5)) {
printUsageAndExit(); printUsageAndExit();
} }
if (argc == 5) {
VLOG(1) << "Module: " << argv[3];
VLOG(1) << "Server: " << argv[4];
} else {
VLOG(1) << "Module: " << argv[1];
VLOG(1) << "Server: " << argv[2];
}
const wchar_t *module, *url; const wchar_t *module, *url;
int timeout = -1; int timeout = -1;
if (argc == 3) { if (argc == 3) {
@ -181,12 +203,15 @@ int wmain(int argc, wchar_t *argv[]) {
} }
} }
VLOG(1) << "Beginning symbol dump";
wstring symbol_file; wstring symbol_file;
PDBModuleInfo pdb_info; PDBModuleInfo pdb_info;
if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) { if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) {
fwprintf(stderr, L"Could not get symbol data from %s\n", module); fwprintf(stderr, L"Could not get symbol data from %s\n", module);
LOG(ERROR) << "Could not get symbol data from " << module;
return 1; return 1;
} }
VLOG(1) << "Symbol dump completed to " << symbol_file;
wstring code_file = WindowsStringUtils::GetBaseName(wstring(module)); wstring code_file = WindowsStringUtils::GetBaseName(wstring(module));

View File

@ -39,7 +39,7 @@
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
Optimization="0" Optimization="0"
AdditionalIncludeDirectories="&quot;$(VSInstallDir)\DIA SDK\include&quot;;..\..\.." AdditionalIncludeDirectories="..\..\..\third_party\glog\glog\src\windows;..\..\..;&quot;$(VSInstallDir)\DIA SDK\include&quot;"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN" PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN"
MinimalRebuild="true" MinimalRebuild="true"
BasicRuntimeChecks="3" BasicRuntimeChecks="3"
@ -115,7 +115,7 @@
/> />
<Tool <Tool
Name="VCCLCompilerTool" Name="VCCLCompilerTool"
AdditionalIncludeDirectories="&quot;$(VSInstallDir)\DIA SDK\include&quot;;..\..\.." AdditionalIncludeDirectories="..\..\..\third_party\glog\glog\src\windows;..\..\..\;&quot;$(VSInstallDir)\DIA SDK\include&quot;"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN" PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN"
RuntimeLibrary="2" RuntimeLibrary="2"
UsePrecompiledHeader="0" UsePrecompiledHeader="0"
@ -192,6 +192,10 @@
RelativePath="..\..\..\common\windows\string_utils-inl.h" RelativePath="..\..\..\common\windows\string_utils-inl.h"
> >
</File> </File>
<File
RelativePath="..\..\..\common\windows\wchar_logging.h"
>
</File>
</Filter> </Filter>
<Filter <Filter
Name="Resource Files" Name="Resource Files"
@ -211,18 +215,94 @@
<File <File
RelativePath="..\..\..\common\windows\http_upload.cc" RelativePath="..\..\..\common\windows\http_upload.cc"
> >
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\..\third_party\glog\glog\src\logging.cc"
>
</File> </File>
<File <File
RelativePath="..\..\..\common\windows\pdb_source_line_writer.cc" RelativePath="..\..\..\common\windows\pdb_source_line_writer.cc"
> >
</File> </File>
<File <File
RelativePath="..\..\..\common\windows\string_utils.cc" RelativePath="..\..\..\third_party\glog\glog\src\windows\port.cc"
> >
</File> </File>
<File
RelativePath="..\..\..\third_party\glog\glog\src\raw_logging.cc"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\..\common\windows\string_utils.cc"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
</File>
<File <File
RelativePath=".\symupload.cc" RelativePath=".\symupload.cc"
> >
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\..\third_party\glog\glog\src\utilities.cc"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
PreprocessorDefinitions="GOOGLE_GLOG_DLL_DECL="
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\..\third_party\glog\glog\src\vlog_is_on.cc"
>
</File> </File>
</Filter> </Filter>
</Files> </Files>