Explorar o código

Add the ability to redirect std::cout and std::cerr into a stringstream.

Sam Jaffe %!s(int64=7) %!d(string=hai) anos
pai
achega
4e4735263c
Modificáronse 2 ficheiros con 279 adicións e 0 borrados
  1. 31 0
      include/scoped_buffer_capture.h
  2. 248 0
      scoped_buffer_capture.xcodeproj/project.pbxproj

+ 31 - 0
include/scoped_buffer_capture.h

@@ -0,0 +1,31 @@
+//
+//  scoped_buffer_capture.h
+//  scoped_buffer_capture
+//
+//  Created by Sam Jaffe on 6/2/18.
+//
+
+#pragma once
+
+class scoped_buffer_capture_t {
+public:
+  scoped_buffer_capture_t(std::ostream & stream)
+  : sbuf(stream.rdbuf())
+  , out(stream) {
+    out.rdbuf(buffer.rdbuf());
+  }
+  
+  ~scoped_buffer_capture_t() {
+    out.rdbuf(sbuf);
+  }
+  
+  std::string str() const { return buffer.str(); }
+private:
+  std::stringstream buffer{};
+  std::streambuf * sbuf;
+  std::ostream & out;
+};
+
+#define capture_stdout() scoped_buffer_capture_t cap_cout(std::cout)
+#define capture_stderr() scoped_buffer_capture_t cap_cerr(std::cerr)
+

+ 248 - 0
scoped_buffer_capture.xcodeproj/project.pbxproj

@@ -0,0 +1,248 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 46;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		CD0C594420C2D85C00454F82 /* scoped_buffer_capture.h in Headers */ = {isa = PBXBuildFile; fileRef = CD0C594320C2D85C00454F82 /* scoped_buffer_capture.h */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+		CD0C593B20C2D81E00454F82 /* libscoped_buffer_capture.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libscoped_buffer_capture.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD0C594320C2D85C00454F82 /* scoped_buffer_capture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scoped_buffer_capture.h; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		CD0C593820C2D81E00454F82 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		CD0C593220C2D81E00454F82 = {
+			isa = PBXGroup;
+			children = (
+				CD0C594220C2D84000454F82 /* include */,
+				CD0C593C20C2D81E00454F82 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		CD0C593C20C2D81E00454F82 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CD0C593B20C2D81E00454F82 /* libscoped_buffer_capture.dylib */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		CD0C594220C2D84000454F82 /* include */ = {
+			isa = PBXGroup;
+			children = (
+				CD0C594320C2D85C00454F82 /* scoped_buffer_capture.h */,
+			);
+			path = include;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+		CD0C593920C2D81E00454F82 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD0C594420C2D85C00454F82 /* scoped_buffer_capture.h in Headers */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+		CD0C593A20C2D81E00454F82 /* scoped_buffer_capture */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD0C593F20C2D81E00454F82 /* Build configuration list for PBXNativeTarget "scoped_buffer_capture" */;
+			buildPhases = (
+				CD0C593720C2D81E00454F82 /* Sources */,
+				CD0C593820C2D81E00454F82 /* Frameworks */,
+				CD0C593920C2D81E00454F82 /* Headers */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = scoped_buffer_capture;
+			productName = scoped_buffer_capture;
+			productReference = CD0C593B20C2D81E00454F82 /* libscoped_buffer_capture.dylib */;
+			productType = "com.apple.product-type.library.dynamic";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		CD0C593320C2D81E00454F82 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0720;
+				ORGANIZATIONNAME = "Sam Jaffe";
+				TargetAttributes = {
+					CD0C593A20C2D81E00454F82 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+				};
+			};
+			buildConfigurationList = CD0C593620C2D81E00454F82 /* Build configuration list for PBXProject "scoped_buffer_capture" */;
+			compatibilityVersion = "Xcode 3.2";
+			developmentRegion = English;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = CD0C593220C2D81E00454F82;
+			productRefGroup = CD0C593C20C2D81E00454F82 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				CD0C593A20C2D81E00454F82 /* scoped_buffer_capture */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+		CD0C593720C2D81E00454F82 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		CD0C593D20C2D81E00454F82 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = dwarf;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				ENABLE_TESTABILITY = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_DYNAMIC_NO_PIC = NO;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_OPTIMIZATION_LEVEL = 0;
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = YES;
+				ONLY_ACTIVE_ARCH = YES;
+				SDKROOT = macosx;
+			};
+			name = Debug;
+		};
+		CD0C593E20C2D81E00454F82 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_NS_ASSERTIONS = NO;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = NO;
+				SDKROOT = macosx;
+			};
+			name = Release;
+		};
+		CD0C594020C2D81E00454F82 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		CD0C594120C2D81E00454F82 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+		CD0C593620C2D81E00454F82 /* Build configuration list for PBXProject "scoped_buffer_capture" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD0C593D20C2D81E00454F82 /* Debug */,
+				CD0C593E20C2D81E00454F82 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD0C593F20C2D81E00454F82 /* Build configuration list for PBXNativeTarget "scoped_buffer_capture" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD0C594020C2D81E00454F82 /* Debug */,
+				CD0C594120C2D81E00454F82 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = CD0C593320C2D81E00454F82 /* Project object */;
+}