import util from 'util';
import path from 'path';

import eventCategories from 'common/analytics/categories';
import eventActions from 'common/analytics/actions';
import eventNames from 'common/analytics/names';

function anonymizeException(err) {
  const app = require('common/electron/app').default;
  err.message = err.message.replace(app.getPath('home'), '<home>');
}

function trimLongPaths(err) {
  const app = require('common/electron/app').default;
  err.stack = err.stack
    .split('\n')
    .map(line => line.replace(/\/.+atom\.asar/, 'atom.asar'))
    .map(line => line.replace(app.getAppPath(), 'app'))
    .join('\n');
}

function namespaceOfFile(filename) {
  const app = require('common/electron/app').default;
  const appPath = path.join(app.getAppPath(), 'scripts') + '/';
  let name = filename.replace(appPath, '').replace('.js', '');
  if (name.indexOf('common/') === 0) {
    name += ':' + process.type;
  }
  return global.manifest.name + ':' + name;
}

function reportToPiwik(namespace, isFatal, err) {
  const piwik = require('common/services/piwik').default.getTracker();
  if (piwik) {
    piwik.trackEvent(
      eventCategories['Logs'],
      eventActions['Exception'],
      isFatal ? eventNames['Fatal Error'] : eventNames['Error'],
      `${namespace}: ${err.name}: ${err.message}`
    );
  }
}

function reportToSentry(namespace, isFatal, err) {
  const sentry = require('common/services/sentry').default;
  if (sentry) {
    anonymizeException(err);
    trimLongPaths(err);

    console.log('reporting to sentry:', err);
    sentry.captureException(err, {
      level: isFatal ? 'fatal' : 'error',
      extra: {
        trace: new Error().stack
      },
      tags: {
        namespace: namespace
      }
    }, function(result) {
      console.log('reported to sentry:', result);
    });
  }
}

export function debugLogger(filename) {
  let logger = null;
  return function() {
    if (!logger) {
      const debug = require('common/modules/debug').default;
      logger = debug(namespaceOfFile(filename));
    }
    const browserLogger = require('common/utils/logger-browser').default;
    logger.log = browserLogger.printDebug;
    logger(util.format(...arguments));
  };
}

export function errorLogger(filename, isFatal, skipReporting) {
  let namespace = null;
  return function(err) {
    if (!namespace) {
      namespace = namespaceOfFile(filename);
    }

    if (!(err instanceof Error)) {
      err = new Error(err);
    }

    const browserLogger = require('common/utils/logger-browser').default;
    browserLogger.printError(namespace, isFatal, util.format(err));

    if (!skipReporting) {
      reportToPiwik(namespace, isFatal, err);
      reportToSentry(namespace, isFatal, err);
    }
  };
}

@@ -788,29 +788,143 @@
 /';%0A
+%0A
   
-let name = filename
+const appPathNorm = path.posix.normalize(appPath);%0A  const filenameNorm = path.posix.normalize(filename);%0A%0A  let name = filenameNorm
 .rep
@@ -935,16 +935,20 @@
 (appPath
+Norm
 , '').re
@@ -980,15 +980,18 @@
 ame.
-indexOf
+startsWith
 ('co
@@ -1001,14 +1001,8 @@
 n/')
- === 0
 ) %7B%0A
@@ -1033,20 +1033,21 @@
 s.type;%0A
-
   %7D%0A
+%0A
   return

Fix logger namespace on win32