Skip to content

Commit

Permalink
add stacktrace sample
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Nov 14, 2014
1 parent 9313b3c commit 377139a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Makefile
Expand Up @@ -9,8 +9,13 @@ all:
test:
$(MAKE) -C test test

sample:
$(MAKE) -C sample

clean:
$(MAKE) -C src clean
$(MAKE) -C test clean
$(MAKE) -C sample clean

.PHONY: sample clean

6 changes: 6 additions & 0 deletions cybozulib.sln
Expand Up @@ -51,6 +51,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "csv_test", "test\base\proj\
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ssl_smpl", "sample\proj\ssl_smpl\ssl_smpl.vcxproj", "{167A43E8-6159-4337-82C2-084551350739}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stacktrace_smpl", "sample\proj\stacktrace_smpl\stacktrace_smpl.vcxproj", "{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -157,6 +159,10 @@ Global
{167A43E8-6159-4337-82C2-084551350739}.Debug|x64.Build.0 = Debug|x64
{167A43E8-6159-4337-82C2-084551350739}.Release|x64.ActiveCfg = Release|x64
{167A43E8-6159-4337-82C2-084551350739}.Release|x64.Build.0 = Release|x64
{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}.Debug|x64.ActiveCfg = Debug|x64
{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}.Debug|x64.Build.0 = Debug|x64
{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}.Release|x64.ActiveCfg = Release|x64
{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions include/cybozu/stacktrace.hpp
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <cybozu/itoa.hpp>
#include <stdlib.h>
#include <iosfwd>

#ifdef _WIN32
#include <winsock2.h>
Expand Down Expand Up @@ -208,6 +209,11 @@ class StackTrace {
}
};

inline std::ostream& operator<<(std::ostream& os, const StackTrace& self)
{
return os << self.toString();
}

} // cybozu

#undef CYBOZU_STACKTRACE_RESOLVE_SYMBOL
2 changes: 1 addition & 1 deletion sample/exception_smpl.cpp
Expand Up @@ -19,7 +19,7 @@ void f2()
MailException e;
e << "can't send" << msg << abc << c << port << s << '\n';
cybozu::StackTrace st;
e << st.toString();
e << st;
throw e;
}

Expand Down
85 changes: 85 additions & 0 deletions sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj
@@ -0,0 +1,85 @@
þ½Ž¿<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{E8D067B3-DA08-401D-BC28-4DDE08FAEFE1}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>stacktrace_smpl</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\..\common.props" />
<Import Project="..\..\..\debug.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\..\common.props" />
<Import Project="..\..\..\release.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\stacktrace_smpl.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
42 changes: 42 additions & 0 deletions sample/stacktrace_smpl.cpp
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cybozu/stacktrace.hpp>

void putStackTrace()
{
std::cout << "no memory!!\n" << cybozu::StackTrace() << std::endl;
exit(1);
}

void func()
{
puts("func");
size_t size = 1024 * 1024;
std::vector<double> v;
for (int i = 0; i < 10240; i++) {
std::cout << "i=" << i << ", size=" << size << std::endl;
v.resize(size);
size *= 16;
}
}

void g()
{
func();
}

void h()
{
g();
}

int main()
try
{
std::set_new_handler(putStackTrace);
h();
} catch (std::exception& e) {
printf("e=%s\n", e.what());
}

0 comments on commit 377139a

Please sign in to comment.