private static void write()

in agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/diagnostics/status/StatusFile.java [190:250]


  private static void write(boolean loggingInitialized) {
    if (!shouldWrite()) {
      return;
    }
    WRITER_THREAD.submit(
        new Runnable() {
          @Override
          public void run() {
            Map<String, Object> map = getJsonMap();

            String fileName = constructFileName(map);

            // the executor should prevent more than one thread from executing this block.
            // this is just a safeguard
            synchronized (lock) {
              File file = new File(directory, fileName);
              boolean dirsWereCreated = file.getParentFile().mkdirs();

              Logger logger = loggingInitialized ? LoggerFactory.getLogger(StatusFile.class) : null;

              if (dirsWereCreated || file.getParentFile().exists()) {
                BufferedSink b = null;
                try {
                  b = getBuffer(file);
                  new Moshi.Builder()
                      .build()
                      .adapter(Map.class)
                      .indent(" ")
                      .nullSafe()
                      .toJson(b, map);
                  b.flush();
                } catch (Exception e) {
                  if (logger != null) {
                    logger.error("Error writing {}", file.getAbsolutePath(), e);
                  } else {
                    e.printStackTrace();
                  }
                  if (b != null) {
                    try {
                      b.close();
                    } catch (IOException ex) {
                      // ignore this
                    }
                  }
                }
              } else {
                if (logger != null) {
                  logger.error(
                      "Parent directories for status file could not be created: {}",
                      file.getAbsolutePath());
                } else {
                  System.err.println(
                      "Parent directories for status file could not be created: "
                          + file.getAbsolutePath());
                }
              }
            }
          }
        },
        "StatusFileJsonWrite");
  }