All files / botbuilder-unit/src/log-reporters BeautyLogReporter.js

87.02% Statements 114/131
54.55% Branches 24/44
90.91% Functions 20/22
87.02% Lines 114/131
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 2061x 1x 1x     1x 1x             1x 1x 1x 1x   1x 1x     1x 21x 21x   1x 12x 12x   1x 8x     8x 8x 8x 8x 8x 8x 8x   1x 59x 13x 13x     59x   1x 13x 13x   1x 13x 13x   13x 13x 13x 16x   13x   1x 16x   16x 18x 18x 16x       18x   18x 18x               1x 1x 1x       1x 1x   1x 2x 2x 1x 1x 1x       1x 1x       1x 7x 7x     7x       7x   7x   1x 1x   1x     1x 3x 3x     3x     3x 3x   1x                     1x 1x 1x 1x   1x 1x   1x 2x 1x   2x     2x 2x 2x   1x 1x 1x     1x 1x   1x 1x     1x 1x     1x 1x           1x 1x   1x   1x 1x 1x 1x 1x 1x     1x
const BaseLogReporter = require('./BaseLogReporter');
const util = require('util');
const colors = require('colors');
 
 
const MIN_COLUMNS_PER_ROW = 40;
const EMPTY_FINAL_REPORT = {
  firstErrorOnStep: false,
  totalSteps: 0,
  warnings: false
}
 
function BeautyLogReporter() {
  this.columnsPerRow = 0;
  this.contentColumnsPerRow = 0;
  this.isLeftPaddingEnabled = false;
  BaseLogReporter.call(this);
}
BeautyLogReporter.prototype = Object.create(BaseLogReporter.prototype);
BeautyLogReporter.prototype.constructor = BeautyLogReporter;
 
 
BeautyLogReporter.prototype.refreshDimensions = function (msg) {
  this.columnsPerRow = Math.max(process.stdout.columns, MIN_COLUMNS_PER_ROW);
  this.contentColumnsPerRow = Math.floor(2 / 3 * this.columnsPerRow);
}
BeautyLogReporter.prototype.normalizeOutput = function (message) {
  let isObject = (message instanceof Object) || ("function" ==  typeof message);;
  return isObject ? util.inspect(message, {depth: 6, color: true, showHidden: true}) :message;
}
BeautyLogReporter.prototype.outputCentralized = function (msg, colorFunc) {
  Iif (!colorFunc) {
    colorFunc = colors.black;
  }
  this.refreshDimensions(msg)
  let offset1 = Math.floor((this.columnsPerRow - msg.length) / 2);
  let offset2 = this.columnsPerRow - offset1 - msg.length;
  let result = '='.repeat(offset1) + msg + '='.repeat(offset2);
  console.log();
  console.log(colorFunc(result));
  console.log();
}
BeautyLogReporter.prototype.log = function (msg, colorFunc) {
  if (this.isLeftPaddingEnabled) {
    Eif (this.columnsPerRow > msg.length) {
      msg = ' '.repeat(this.columnsPerRow - msg.length) + msg;
    }
  }
  console.log(colorFunc(msg));
}
BeautyLogReporter.prototype.getDate = function () {
  let date = new Date();
  return date.toTimeString().split(' ')[0];
}
BeautyLogReporter.prototype.outputMessageBox = function (step, msg, title, colorFunc) {
  this.refreshDimensions(msg);
  let lines = msg.split("\n");
 
  this.log('#' + step + ' ' + this.getDate() + `, ${title}`, colors.black);
  this.log(('+' + '-'.repeat(this.contentColumnsPerRow - 2) + '+'), colorFunc);
  lines.forEach((item) => {
    this.wrap(item, colorFunc);
  })
  this.log(('+' + '-'.repeat(this.contentColumnsPerRow - 2) + '+'), colorFunc);
}
BeautyLogReporter.prototype.wrap = function (msg, colorFunc) {
  let moreThanOnce = false;
 
  do  {
    let chunk = msg.substr(0, this.contentColumnsPerRow - 4);
    if (chunk.length < this.contentColumnsPerRow - 4) {
      chunk = chunk + ' '.repeat(this.contentColumnsPerRow - 4 - chunk.length);
 
    } else {
    }
    chunk = '| ' + chunk + ' |';
 
    msg = msg.substr(this.contentColumnsPerRow - 4);
    this.log(chunk, colorFunc);
  } while (msg.length > 0);
 
}
 
// ---------------------------------------------------------------------------------------------------------------------
// Base Reporter Inherited methods
 
BeautyLogReporter.prototype.newScript = function (messages, scriptName) {
  this.finalReport = Object.assign({}, EMPTY_FINAL_REPORT);
  Iif (scriptName) {
    this.outputCentralized(`NEW SCRIPT: ${scriptName.toUpperCase()}`, colors.blue);
  }
 
  this.finalReport.totalSteps = messages.length;
  BaseLogReporter.prototype.newScript.call(this, messages);
}
BeautyLogReporter.prototype.scriptFinished = function () {
  this.isLeftPaddingEnabled = false;
  if (false !== this.finalReport.firstErrorOnStep) {
    this.outputCentralized(' SCRIPT FINISHED WITH ERRORS ', colors.red);
    this.log(`Completed / Total steps: ${this.finalReport.firstErrorOnStep}/${this.finalReport.totalSteps}`, colors.red)
  } else Iif (this.finalReport.warnings) {
    this.outputCentralized(' SCRIPT FINISHED WITH WARNINGS', colors.yellow);
    this.log(`Completed / Total steps: ${this.finalReport.totalSteps}/${this.finalReport.totalSteps}`, colors.yellow);
  } else {
    this.outputCentralized(' SCRIPT FINISHED SUCCESSFULLY', colors.green);
    this.log(`Completed / Total steps: ${this.finalReport.totalSteps}/${this.finalReport.totalSteps}`, colors.green);
  }
 
}
BeautyLogReporter.prototype.messageReceived = function (step, message) {
  let outputMessage = this.normalizeOutput(message);
  Iif ( message.attachmentLayout ) {
    outputMessage += `\n-- attachmentLayout:${message.attachmentLayout}`
  }
  Iif ( message.attachments ) {
    outputMessage += `\n-- attachments:\n${util.inspect(message.attachments)}`
  }
  //let outputMessage = this.normalizeOutput(message);
  this.isLeftPaddingEnabled = false;
 
  this.outputMessageBox(step, outputMessage, 'Bot wrote:', colors.blue);
}
BeautyLogReporter.prototype.endConversation = function (step) {
  this.outputCentralized(` STEP #${step}, END OF CONVERSATION `, colors.blue);
}
BeautyLogReporter.prototype.typing = function (step) {
  this.outputCentralized(` STEP #${step}, TYPING ... `, colors.blue);
}
BeautyLogReporter.prototype.messageSent = function (step, message) {
  let outputMessage = '';
  Iif ("function" == typeof message.user) {
    outputMessage = message.user.toString();
  } else {
    outputMessage = message.user ? message.user : this.normalizeOutput(message);
  }
 
  this.isLeftPaddingEnabled = true;
  this.outputMessageBox(step, outputMessage, 'User wrote:', colors.green);
}
BeautyLogReporter.prototype.customStep = function (step, message) {
  let outputMessage = '';
  if ("function" == typeof message.custom) {
    outputMessage = message.custom.toString();
  } else {
    outputMessage = message.custom ? message.custom : this.normalizeOutput(message);
  }
  this.isLeftPaddingEnabled = true;
  this.outputMessageBox(step, outputMessage, 'Custom Validation Step:', colors.yellow);
 
}
BeautyLogReporter.prototype.expectationError = function (step, received, error ) {
  Eif (false === this.finalReport.firstErrorOnStep) {
    this.finalReport.firstErrorOnStep = step;
    this.finalReport.error = error;
  }
  this.outputCentralized(`EXPECTATION ERROR ON STEP #${step}`, colors.red);
  this.error('Error:', error);
}
BeautyLogReporter.prototype.error = function (errorHeader, message) {
  if (!this.finalReport.error) {
    this.finalReport.error = message;
  }
  Iif ( "undefined" == typeof errorHeader ) {
    errorHeader = 'ERROR';
  }
  this.outputCentralized(errorHeader, colors.red);
  console.log(colors.red(this.normalizeOutput(message)));
  this.isLeftPaddingEnabled = false;
}
BeautyLogReporter.prototype.warning = function (warningHeader, message) {
  this.finalReport.warnings = true;
  Iif ( "undefined" == typeof warningHeader ) {
    warningHeader = 'WARNING';
  }
  this.outputCentralized(warningHeader, colors.yellow);
  console.log(colors.yellow(message));
}
BeautyLogReporter.prototype.info = function (infoHeader, message ) {
  Iif ( "undefined" == typeof infoHeader ) {
    infoHeader = 'INFO';
  }
  this.outputCentralized(infoHeader, colors.yellow);
  console.log(colors.yellow(this.normalizeOutput(message)));
}
 
BeautyLogReporter.prototype.session = function (step, message) {
  let output = {
    userData : Object.assign({}, message.userData),
    conversationData : Object.assign({}, message.conversationData),
    privateConversationData : Object.assign({}, message.privateConversationData),
    sessionState : Object.assign({}, message.sessionState)
  }
  output = this.normalizeOutput(output);
  this.isLeftPaddingEnabled = false;
 
  this.outputMessageBox(step, output, 'Session set:', colors.blue);
}
BeautyLogReporter.prototype.startupDialog = function (step, dialog, args) {
  this.isLeftPaddingEnabled = false;
  dialog = dialog || '';
  args = args || '';
  this.outputMessageBox(step, dialog, 'Next Dialog:', colors.blue);
  this.outputMessageBox(step, this.normalizeOutput(args), 'ARGS Set:', colors.blue);
}
 
module.exports = BeautyLogReporter;