sim: Generate logging within the sim

Add logging support for when running in the simulator.  Log messages are
still based on compile-time determinations, and log using printf.

Based on a patch from Marti Bolivar <marti.bolivar@linaro.org>.
diff --git a/boot/bootutil/include/bootutil/bootutil_log.h b/boot/bootutil/include/bootutil/bootutil_log.h
index 6dca9b3..41370a8 100644
--- a/boot/bootutil/include/bootutil/bootutil_log.h
+++ b/boot/bootutil/include/bootutil/bootutil_log.h
@@ -62,9 +62,54 @@
 #include <logging/sys_log.h>
 
 /*
+ * When built on the simulator, just use printf().
+ */
+#elif defined(__BOOTSIM__)	/* !defined(__ZEPHYR__) */
+
+#include <stdio.h>
+
+#define BOOT_LOG_LEVEL_OFF	0
+#define BOOT_LOG_LEVEL_ERROR	1
+#define BOOT_LOG_LEVEL_WARNING	2
+#define BOOT_LOG_LEVEL_INFO	3
+#define BOOT_LOG_LEVEL_DEBUG	4
+
+#ifndef BOOT_LOG_LEVEL
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_ERROR
+#define BOOT_LOG_ERR(...)						\
+    do { printf("[ERR] " __VA_ARGS__); printf("\n"); } while (0)
+#else
+#define BOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_WARNING
+#define BOOT_LOG_WRN(...)                                               \
+    do { printf("[WRN] " __VA_ARGS__); printf("\n"); } while (0)
+#else
+#define BOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_INFO
+#define BOOT_LOG_INF(...)                                               \
+    do { printf("[INF] " __VA_ARGS__); printf("\n"); } while (0)
+#else
+#define BOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_DEBUG
+#define BOOT_LOG_DBG(...)                                               \
+    do { printf("[DBG] " __VA_ARGS__); printf("\n"); } while (0)
+#else
+#define BOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
+#endif
+
+/*
  * In other environments, logging calls are no-ops.
  */
-#else  /* !defined(__ZEPHYR__) */
+#else  /* !defined(__BOOTSIM__) */
 
 #define BOOT_LOG_LEVEL_OFF	0
 #define BOOT_LOG_LEVEL_ERROR	1
diff --git a/sim/build.rs b/sim/build.rs
index 74e271c..839789b 100644
--- a/sim/build.rs
+++ b/sim/build.rs
@@ -15,6 +15,7 @@
     conf.include("../boot/bootutil/include");
     conf.include("../boot/zephyr/include");
     conf.debug(true);
+    conf.define("__BOOTSIM__", None);
     conf.compile("libbootutil.a");
     walk_dir("../boot").unwrap();
     walk_dir("csupport").unwrap();
diff --git a/sim/csupport/run.c b/sim/csupport/run.c
index c9743fe..06bc558 100644
--- a/sim/csupport/run.c
+++ b/sim/csupport/run.c
@@ -10,6 +10,9 @@
 
 #include "../../boot/bootutil/src/bootutil_priv.h"
 
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_DEBUG
+#include <bootutil/bootutil_log.h>
+
 extern int sim_flash_erase(void *flash, uint32_t offset, uint32_t size);
 extern int sim_flash_read(void *flash, uint32_t offset, uint8_t *dest, uint32_t size);
 extern int sim_flash_write(void *flash, uint32_t offset, const uint8_t *src, uint32_t size);
@@ -133,6 +136,8 @@
 int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
 		    uint32_t len)
 {
+	BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x",
+		     __func__, area->fa_id, off, len);
 	return hal_flash_read(area->fa_id,
 			      area->fa_off + off,
 			      dst, len);
@@ -141,6 +146,7 @@
 int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
 		     uint32_t len)
 {
+	BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
 	return hal_flash_write(area->fa_id,
 			       area->fa_off + off,
 			       src, len);
@@ -148,6 +154,7 @@
 
 int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
 {
+	BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
 	return hal_flash_erase(area->fa_id,
 			       area->fa_off + off,
 			       len);