logging: add a small example program
authorAdam Simpkins <simpkins@fb.com>
Tue, 20 Jun 2017 18:01:53 +0000 (11:01 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 20 Jun 2017 18:06:44 +0000 (11:06 -0700)
Summary:
This adds a small example program which demonstrates using the logging library.

This gives a very basic example of how the library is intended to be used, and
can also be used to play around with controlling the log levels from the
command line argument.

Reviewed By: wez

Differential Revision: D5083104

fbshipit-source-id: ab09c6c88db33065f6e39f35b28014f2a6153cef

.gitignore
folly/configure.ac
folly/experimental/logging/Makefile.am
folly/experimental/logging/example/Makefile.am [new file with mode: 0644]
folly/experimental/logging/example/lib.cpp [new file with mode: 0644]
folly/experimental/logging/example/lib.h [new file with mode: 0644]
folly/experimental/logging/example/main.cpp [new file with mode: 0644]

index 775b535..08702cc 100644 (file)
@@ -22,6 +22,7 @@ folly/**/test/*_test_using_jemalloc
 folly/**/test/*.trs
 folly/config.*
 folly/configure
+folly/experimental/logging/example/logging_example
 folly/libfolly.pc
 folly/m4/libtool.m4
 folly/m4/ltoptions.m4
index e7c73c8..2d5ca29 100644 (file)
@@ -608,6 +608,7 @@ AC_CONFIG_FILES([Makefile
                  experimental/Makefile
                  experimental/io/test/Makefile
                  experimental/logging/Makefile
+                 experimental/logging/example/Makefile
                  experimental/symbolizer/Makefile
                  init/Makefile
                  stats/test/Makefile])
index bd38e61..a758c3f 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = .
+SUBDIRS = . example
 
 lib_LTLIBRARIES = libfollylogging.la
 
diff --git a/folly/experimental/logging/example/Makefile.am b/folly/experimental/logging/example/Makefile.am
new file mode 100644 (file)
index 0000000..d74ae4d
--- /dev/null
@@ -0,0 +1,9 @@
+noinst_PROGRAMS = logging_example
+noinst_LTLIBRARIES = libfollylogging_example.la
+
+logging_example_SOURCES = main.cpp
+logging_example_LDADD = libfollylogging_example.la
+
+libfollylogging_example_la_SOURCES = lib.cpp
+libfollylogging_example_la_LIBADD = \
+       $(top_builddir)/experimental/logging/libfollylogging.la
diff --git a/folly/experimental/logging/example/lib.cpp b/folly/experimental/logging/example/lib.cpp
new file mode 100644 (file)
index 0000000..7190c70
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <folly/experimental/logging/example/lib.h>
+
+namespace example {
+ExampleObject::~ExampleObject() {
+  // All XLOG() statements in this file will log to the category
+  // folly.experimental.logging.example.lib
+  XLOGF(DBG1, "ExampleObject({}) at {} destroyed", value_, this);
+}
+}
diff --git a/folly/experimental/logging/example/lib.h b/folly/experimental/logging/example/lib.h
new file mode 100644 (file)
index 0000000..c43a88f
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <folly/Range.h>
+#include <folly/experimental/logging/xlog.h>
+
+namespace example {
+
+class ExampleObject {
+ public:
+  explicit ExampleObject(folly::StringPiece str) : value_{str.str()} {
+    // All XLOG() statements in this file will log to the category
+    // folly.experimental.logging.example.lib
+    XLOGF(DBG1, "ExampleObject({}) constructed at {}", value_, this);
+  }
+  ~ExampleObject();
+
+  void doStuff();
+
+ private:
+  std::string value_;
+};
+}
diff --git a/folly/experimental/logging/example/main.cpp b/folly/experimental/logging/example/main.cpp
new file mode 100644 (file)
index 0000000..14d0883
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2004-present Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <folly/experimental/logging/Init.h>
+#include <folly/experimental/logging/xlog.h>
+#include <folly/init/Init.h>
+
+#include <folly/experimental/logging/example/lib.h>
+
+DEFINE_string(logging, "", "Logging category configuration string");
+
+using namespace example;
+using folly::LogLevel;
+
+// Invoking code that uses XLOG() statements before main is safe,
+// but will not log anywhere, since no handlers are configured yet.
+static ExampleObject staticInitialized("static");
+
+int main(int argc, char* argv[]) {
+  // Using log macros before configuring any log levels or log handlers is
+  // safe, but the messages will always be ignore since no handlers are defined.
+  XLOG(INFO, "no handlers configured yet, so this will go nowhere");
+  printf("main starting\n");
+  fflush(stdout);
+
+  // Call folly::init() and then initialize log levels and handlers
+  folly::init(&argc, &argv);
+  initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO);
+
+  // All XLOG() statements in this file will log to the category
+  // folly.experimental.logging.example.main
+  XLOG(INFO, "now log messages will be sent to stderr");
+
+  XLOG(DBG1, "log arguments are concatenated: ", 12345, ", ", 92.0);
+  XLOGF(DBG1, "XLOGF supports {}-style formatting: {:.3f}", "python", 1.0 / 3);
+  XLOG(DBG2) << "streaming syntax is also supported: " << 1234;
+  XLOG(DBG2, "you can even", " mix function-style") << " and streaming "
+                                                    << "syntax";
+
+  ExampleObject("foo");
+  XLOG(INFO, "main returning");
+  return 0;
+}