Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * @license Copyright 2016 Google Inc. All Rights Reserved.
  3. * 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
  4. * 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.
  5. */
  6. 'use strict';
  7. const debug = require('debug');
  8. const marky = require('marky');
  9. const EventEmitter = require('events').EventEmitter;
  10. const isWindows = process.platform === 'win32';
  11. // process.browser is set when browserify'd via the `process` npm module
  12. const isBrowser = process.browser;
  13. const colors = {
  14. red: isBrowser ? 'crimson' : 1,
  15. yellow: isBrowser ? 'gold' : 3,
  16. cyan: isBrowser ? 'darkturquoise' : 6,
  17. green: isBrowser ? 'forestgreen' : 2,
  18. blue: isBrowser ? 'steelblue' : 4,
  19. magenta: isBrowser ? 'palevioletred' : 5,
  20. };
  21. // whitelist non-red/yellow colors for debug()
  22. debug.colors = [colors.cyan, colors.green, colors.blue, colors.magenta];
  23. class Emitter extends EventEmitter {
  24. /**
  25. * Fires off all status updates. Listen with
  26. * `require('lib/log').events.addListener('status', callback)`
  27. * @param {string} title
  28. * @param {!Array<*>} argsArray
  29. */
  30. issueStatus(title, argsArray) {
  31. if (title === 'status' || title === 'statusEnd') {
  32. this.emit(title, [title, ...argsArray]);
  33. }
  34. }
  35. /**
  36. * Fires off all warnings. Listen with
  37. * `require('lib/log').events.addListener('warning', callback)`
  38. * @param {string} title
  39. * @param {!Array<*>} argsArray
  40. */
  41. issueWarning(title, argsArray) {
  42. this.emit('warning', [title, ...argsArray]);
  43. }
  44. }
  45. const loggersByTitle = {};
  46. const loggingBufferColumns = 25;
  47. let level_;
  48. class Log {
  49. static _logToStdErr(title, argsArray) {
  50. const log = Log.loggerfn(title);
  51. log(...argsArray);
  52. }
  53. static loggerfn(title) {
  54. let log = loggersByTitle[title];
  55. if (!log) {
  56. log = debug(title);
  57. loggersByTitle[title] = log;
  58. // errors with red, warnings with yellow.
  59. if (title.endsWith('error')) {
  60. log.color = colors.red;
  61. } else if (title.endsWith('warn')) {
  62. log.color = colors.yellow;
  63. }
  64. }
  65. return log;
  66. }
  67. /**
  68. * @param {string} level
  69. */
  70. static setLevel(level) {
  71. level_ = level;
  72. switch (level) {
  73. case 'silent':
  74. debug.enable('-*');
  75. break;
  76. case 'verbose':
  77. debug.enable('*');
  78. break;
  79. case 'error':
  80. debug.enable('-*, *:error');
  81. break;
  82. default:
  83. debug.enable('*, -*:verbose');
  84. }
  85. }
  86. /**
  87. * A simple formatting utility for event logging.
  88. * @param {string} prefix
  89. * @param {!Object} data A JSON-serializable object of event data to log.
  90. * @param {string=} level Optional logging level. Defaults to 'log'.
  91. */
  92. static formatProtocol(prefix, data, level) {
  93. const columns = (!process || process.browser) ? Infinity : process.stdout.columns;
  94. const method = data.method || '?????';
  95. const maxLength = columns - method.length - prefix.length - loggingBufferColumns;
  96. // IO.read blacklisted here to avoid logging megabytes of trace data
  97. const snippet = (data.params && method !== 'IO.read') ?
  98. JSON.stringify(data.params).substr(0, maxLength) : '';
  99. Log._logToStdErr(`${prefix}:${level || ''}`, [method, snippet]);
  100. }
  101. /**
  102. * @return {boolean}
  103. */
  104. static isVerbose() {
  105. return level_ === 'verbose';
  106. }
  107. static time({msg, id, args = []}, level = 'log') {
  108. marky.mark(id);
  109. Log[level]('status', msg, ...args);
  110. }
  111. static timeEnd({msg, id, args = []}, level = 'verbose') {
  112. Log[level]('statusEnd', msg, ...args);
  113. marky.stop(id);
  114. }
  115. static log(title, ...args) {
  116. Log.events.issueStatus(title, args);
  117. return Log._logToStdErr(title, args);
  118. }
  119. static warn(title, ...args) {
  120. Log.events.issueWarning(title, args);
  121. return Log._logToStdErr(`${title}:warn`, args);
  122. }
  123. static error(title, ...args) {
  124. return Log._logToStdErr(`${title}:error`, args);
  125. }
  126. static verbose(title, ...args) {
  127. Log.events.issueStatus(title, args);
  128. return Log._logToStdErr(`${title}:verbose`, args);
  129. }
  130. /**
  131. * Add surrounding escape sequences to turn a string green when logged.
  132. * @param {string} str
  133. * @return {string}
  134. */
  135. static greenify(str) {
  136. return `${Log.green}${str}${Log.reset}`;
  137. }
  138. /**
  139. * Add surrounding escape sequences to turn a string red when logged.
  140. * @param {string} str
  141. * @return {string}
  142. */
  143. static redify(str) {
  144. return `${Log.red}${str}${Log.reset}`;
  145. }
  146. static get green() {
  147. return '\x1B[32m';
  148. }
  149. static get red() {
  150. return '\x1B[31m';
  151. }
  152. static get yellow() {
  153. return '\x1b[33m';
  154. }
  155. static get purple() {
  156. return '\x1b[95m';
  157. }
  158. static get reset() {
  159. return '\x1B[0m';
  160. }
  161. static get bold() {
  162. return '\x1b[1m';
  163. }
  164. static get dim() {
  165. return '\x1b[2m';
  166. }
  167. static get tick() {
  168. return isWindows ? '\u221A' : '✓';
  169. }
  170. static get cross() {
  171. return isWindows ? '\u00D7' : '✘';
  172. }
  173. static get whiteSmallSquare() {
  174. return isWindows ? '\u0387' : '▫';
  175. }
  176. static get heavyHorizontal() {
  177. return isWindows ? '\u2500' : '━';
  178. }
  179. static get heavyVertical() {
  180. return isWindows ? '\u2502 ' : '┃ ';
  181. }
  182. static get heavyUpAndRight() {
  183. return isWindows ? '\u2514' : '┗';
  184. }
  185. static get heavyVerticalAndRight() {
  186. return isWindows ? '\u251C' : '┣';
  187. }
  188. static get heavyDownAndHorizontal() {
  189. return isWindows ? '\u252C' : '┳';
  190. }
  191. static get doubleLightHorizontal() {
  192. return '──';
  193. }
  194. }
  195. Log.events = new Emitter();
  196. Log.takeTimeEntries = () => {
  197. const entries = marky.getEntries();
  198. marky.clear();
  199. return entries;
  200. };
  201. Log.getTimeEntries = () => marky.getEntries();
  202. module.exports = Log;