From 3ab6e0425a97484186774696e62bb61002d996b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Tozatti=20Risso?= <jvtr12@inf.ufpr.br>
Date: Thu, 15 Sep 2016 11:33:48 -0300
Subject: [PATCH] Change directory creation to gulpfile
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: João Victor Tozatti Risso <jvtr12@inf.ufpr.br>
---
 gulpfile.babel.js | 29 +++++++++++++++++++++--------
 src/libs/log.js   | 30 +++++++++++++++---------------
 2 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/gulpfile.babel.js b/gulpfile.babel.js
index 37ab19c3..78fda960 100644
--- a/gulpfile.babel.js
+++ b/gulpfile.babel.js
@@ -1,3 +1,5 @@
+const fs = require('fs');
+
 const gulp = require('gulp');
 
 const babel = require('gulp-babel');
@@ -12,10 +14,23 @@ const Cache = require('gulp-file-cache');
 
 const cache = new Cache();
 
+function createLogDir() {
+    const logDirPath = 'build/logs';
+    fs.access(logDirPath, fs.F_OK, (err) => {
+        if (err) {
+            console.info(`Logs directory not found, creating it.`);
+            fs.mkdir(logDirPath, 0o700, (dirErr) => {
+                console.error(`Failed to create logs directory.\n\tError: ${dirErr}`);
+            });
+        }
+    });
+}
+
 /**
  * Compile source files
  */
-function compile() {
+gulp.task('compile', () => {
+    createLogDir();
     // run ESLint
     gulp.src('src/**/*.js')
         .pipe(eslint())
@@ -31,10 +46,9 @@ function compile() {
     // copy configuration file to build directory
     gulp.src('config.json')
         .pipe(gulp.dest('build'));
+});
 
-}
-
-gulp.task('build', compile);
+gulp.task('build', ['compile']);
 
 gulp.task('test', ['build'], () => {
   gulp.src('test/test.js', {read: false})
@@ -47,13 +61,12 @@ gulp.task('test', ['build'], () => {
     });
 });
 
-gulp.task('watch', [], () => {
+gulp.task('watch', ['compile'], () => {
     console.log('Watching source directory for changes');
-    compile();
     gulp.watch('src/**/*.js').on('change', () => {
         console.log('Recompiling source');
-        compile();
-        console.log('Source recompilation done')
+        gulp.start('compile');
+        console.log('Source recompilation done');
     });
 });
 
diff --git a/src/libs/log.js b/src/libs/log.js
index befd5ad3..e71edf14 100644
--- a/src/libs/log.js
+++ b/src/libs/log.js
@@ -2,8 +2,6 @@ const config = require('./config');
 
 const winston = require('winston');
 
-const fs = require('fs');
-
 winston.emitErrs = true;
 
 function getFilePath(module) {
@@ -13,25 +11,28 @@ function getFilePath(module) {
 
 function logger(module) {
     const logPath = `${process.cwd()}/logs`;
-    /* Check if logs directory exists */
-    try {
-        fs.accessSync(logPath, fs.F_OK, () => {});
-    } catch (err) {
-        // if logs directory does not exist, create it.
-        fs.mkdirSync(logPath, 0o700, (dirErr) => {
-            throw new Error(`Failed to create logs directory!\n\tError: ${dirErr}`);
-        });
-    }
+    const maxLogFiles = 5;
+    const maxLogSize = 5242880;
     const log = new winston.Logger({
         transports: [
             new winston.transports.File({
                 name: 'info-log',
                 level: 'info',
-                filename: `${logPath}/all.log`,
+                filename: `${logPath}/simcaq-info.log`,
+                handleException: true,
+                json: false,
+                maxSize: maxLogSize, // 5MB
+                maxFiles: maxLogFiles,
+                colorize: false,
+            }),
+            new winston.transports.File({
+                name: 'error-log',
+                level: 'error',
+                filename: `${logPath}/simcaq-error.log`,
                 handleException: true,
                 json: false,
-                maxSize: 5242880, // 5MB
-                maxFiles: 2,
+                maxSize: maxLogSize, // 5MB
+                maxFiles: maxLogFiles,
                 colorize: false,
             }),
             new winston.transports.Console({
@@ -45,7 +46,6 @@ function logger(module) {
         ],
         exitOnError: false,
     });
-
     const debugMode = (typeof config.get('debug') === 'undefined') ?
         config.get('debug') : false;
     if (!debugMode) {
-- 
GitLab