Skip to content

Commit c18cf9b

Browse files
committed
feat(log): configurable logging verbosity; fixes #134
1 parent 8ec1647 commit c18cf9b

File tree

8 files changed

+73
-25
lines changed

8 files changed

+73
-25
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ const automizer = new Automizer({
175175

176176
// use a callback function to track pptx generation process.
177177
// statusTracker: myStatusTracker,
178+
179+
// Use 1 to show warnings or 2 for detailed information
180+
// 0 disables logging
181+
verbosity: 1
178182
});
179183

180184
// Now we can start and load a pptx template.

__tests__/add-slide-master.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test('Append and modify slideMastes and use slideLayouts', async () => {
55
const automizer = new Automizer({
66
templateDir: `${__dirname}/pptx-templates`,
77
outputDir: `${__dirname}/pptx-output`,
8+
verbosity: 2,
89
});
910

1011
const pres = await automizer

src/automizer.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { PresTemplate } from './interfaces/pres-template';
1313
import { RootPresTemplate } from './interfaces/root-pres-template';
1414
import { Template } from './classes/template';
1515
import { ModifyXmlCallback, TemplateInfo } from './types/xml-types';
16-
import { GeneralHelper, vd } from './helper/general-helper';
16+
import { GeneralHelper, log, Logger } from './helper/general-helper';
1717
import { Master } from './classes/master';
1818
import path from 'path';
1919
import * as fs from 'fs';
@@ -112,11 +112,15 @@ export default class Automizer implements IPresentationProps {
112112
this.templates.push(newTemplate);
113113
});
114114
}
115+
116+
if (params.verbosity) {
117+
Logger.verbosity = params.verbosity;
118+
}
115119
}
116120

117121
setStatusTracker(statusTracker: StatusTracker['next']): void {
118122
const defaultStatusTracker = (status: StatusTracker) => {
119-
console.log(status.info + ' (' + status.share + '%)');
123+
log(status.info + ' (' + status.share + '%)', 2);
120124
};
121125

122126
this.status = {
@@ -570,13 +574,13 @@ export default class Automizer implements IPresentationProps {
570574
return this.templateFallbackDir + location;
571575
} else {
572576
if (typeof location === 'string') {
573-
vd('No file matches "' + location + '"');
577+
log('No file matches "' + location + '"', 0);
574578
} else {
575-
vd('Invalid filename');
579+
log('Invalid filename', 0);
576580
}
577581

578-
vd('@templateDir: ' + this.templateDir);
579-
vd('@templateFallbackDir: ' + this.templateFallbackDir);
582+
log('@templateDir: ' + this.templateDir, 2);
583+
log('@templateFallbackDir: ' + this.templateFallbackDir, 2);
580584
}
581585
break;
582586
case 'output':

src/classes/layout.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import { ShapeTargetType, SourceIdentifier, Target } from '../types/types';
44
import { IPresentationProps } from '../interfaces/ipresentation-props';
55
import { PresTemplate } from '../interfaces/pres-template';
66
import { RootPresTemplate } from '../interfaces/root-pres-template';
7-
import { HelperElement } from '../types/xml-types';
8-
import IArchive from '../interfaces/iarchive';
9-
import { IMaster } from '../interfaces/imaster';
107
import { XmlRelationshipHelper } from '../helper/xml-relationship-helper';
118
import HasShapes from './has-shapes';
129
import { ILayout } from '../interfaces/ilayout';
13-
import { Chart } from '../shapes/chart';
14-
import { Image } from '../shapes/image';
15-
import { vd } from '../helper/general-helper';
10+
import { log } from '../helper/general-helper';
1611

1712
export class Layout extends HasShapes implements ILayout {
1813
targetType: ShapeTargetType = 'slideLayout';
@@ -48,7 +43,7 @@ export class Layout extends HasShapes implements ILayout {
4843
this.targetRelsPath = `ppt/slideLayouts/_rels/slideLayout${this.targetNumber}.xml.rels`;
4944
this.sourceArchive = await this.sourceTemplate.archive;
5045

51-
console.log('Importing slideLayout ' + this.targetNumber);
46+
log('Importing slideLayout ' + this.targetNumber, 2);
5247

5348
await this.copySlideLayoutFiles();
5449
await this.copyRelatedContent();

src/classes/master.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import { FileHelper } from '../helper/file-helper';
22
import { XmlHelper } from '../helper/xml-helper';
3-
import {
4-
ShapeTargetType,
5-
SlideModificationCallback,
6-
SourceIdentifier,
7-
Target,
8-
} from '../types/types';
3+
import { ShapeTargetType, SourceIdentifier, Target } from '../types/types';
94
import { IPresentationProps } from '../interfaces/ipresentation-props';
105
import { PresTemplate } from '../interfaces/pres-template';
116
import { RootPresTemplate } from '../interfaces/root-pres-template';
12-
import { HelperElement, XmlElement } from '../types/xml-types';
7+
import { XmlElement } from '../types/xml-types';
138
import IArchive from '../interfaces/iarchive';
149
import { IMaster } from '../interfaces/imaster';
1510
import { XmlRelationshipHelper } from '../helper/xml-relationship-helper';
1611
import HasShapes from './has-shapes';
1712
import { Layout } from './layout';
13+
import { log } from '../helper/general-helper';
1814

1915
export class Master extends HasShapes implements IMaster {
2016
targetType: ShapeTargetType = 'slideMaster';
@@ -60,7 +56,7 @@ export class Master extends HasShapes implements IMaster {
6056
this.targetRelsPath = `ppt/slideMasters/_rels/slideMaster${this.targetNumber}.xml.rels`;
6157
this.sourceArchive = await this.sourceTemplate.archive;
6258

63-
console.log('Importing slideMaster ' + this.targetNumber);
59+
log('Importing slideMaster ' + this.targetNumber, 2);
6460

6561
await this.copySlideMasterFiles();
6662
await this.copyRelatedLayouts();

src/helper/general-helper.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,42 @@ export const vd = (v: any, keys?: boolean): void => {
2626
};
2727

2828
export const last = <T>(arr: T[]): T => arr[arr.length - 1];
29+
30+
export interface Logger {
31+
verbosity: 0 | 1 | 2;
32+
target: 'console' | 'file';
33+
log: (
34+
message: string,
35+
verbosity: Logger['verbosity'],
36+
showStack?: boolean,
37+
target?: Logger['target'],
38+
) => void;
39+
}
40+
41+
export const Logger = <Logger>{
42+
verbosity: 1,
43+
target: 'console',
44+
log: (message, verbosity, showStack?, target?) => {
45+
if (verbosity > Logger.verbosity) {
46+
return;
47+
}
48+
target = target || Logger.target;
49+
if (target === 'console') {
50+
if (showStack) {
51+
vd(message);
52+
} else {
53+
console.log(message);
54+
}
55+
} else {
56+
// TODO: append message to a logfile
57+
}
58+
},
59+
};
60+
61+
export const log = (message: string, verbosity: Logger['verbosity']) => {
62+
Logger.log(message, verbosity);
63+
};
64+
65+
export const logDebug = (message: string, verbosity: Logger['verbosity']) => {
66+
Logger.log(message, verbosity, true);
67+
};

src/helper/xml-helper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import {
1111
import { TargetByRelIdMap } from '../constants/constants';
1212
import { XmlPrettyPrint } from './xml-pretty-print';
1313
import { GetRelationshipsCallback, Target } from '../types/types';
14-
import { vd } from './general-helper';
14+
import { log } from './general-helper';
1515
import { contentTracker } from './content-tracker';
1616
import IArchive from '../interfaces/iarchive';
1717
import {
1818
ContentTypeExtension,
1919
ContentTypeMap,
2020
} from '../enums/content-type-map';
21-
import XmlElements from './xml-elements';
2221

2322
export class XmlHelper {
2423
static async modifyXmlInArchive(
@@ -166,7 +165,9 @@ export class XmlHelper {
166165
}
167166

168167
static async getRelationshipTargetsByPrefix(
169-
archive: IArchive, path: string, prefix: string | string[]
168+
archive: IArchive,
169+
path: string,
170+
prefix: string | string[],
170171
): Promise<Target[]> {
171172
const prefixes = typeof prefix === 'string' ? [prefix] : prefix;
172173
return XmlHelper.getRelationshipItems(
@@ -573,7 +574,7 @@ archive: IArchive, path: string, prefix: string | string[]
573574
const parent = collection[0].parentNode;
574575
order.forEach((index, i) => {
575576
if (!collection[index]) {
576-
vd('sortCollection index not found' + index);
577+
log('sortCollection index not found' + index, 1);
577578
return;
578579
}
579580

src/types/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import IArchive, { ArchiveMode } from '../interfaces/iarchive';
1111
import { ContentTypeExtension } from '../enums/content-type-map';
1212
import PptxGenJS from 'pptxgenjs';
13+
import { Logger } from '../helper/general-helper';
1314

1415
export type ShapeTargetType = 'slide' | 'slideMaster' | 'slideLayout';
1516
export type SourceIdentifier = number | string;
@@ -109,6 +110,13 @@ export type AutomizerParams = {
109110
* You can e.g. attach a custom callback to a progress bar.
110111
*/
111112
statusTracker?: StatusTracker['next'];
113+
/**
114+
* Set logging verbosity.
115+
* 0: no logging at all
116+
* 1: show warnings
117+
* 2: show info (e.g. on import & append)
118+
*/
119+
verbosity?: Logger['verbosity'];
112120
};
113121
export type StatusTracker = {
114122
current: number;

0 commit comments

Comments
 (0)