Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT/DO NOT REVIEW] Kx/debugging discoverability poc #2529

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions apps/teams-test-app/src/components/LoggerAPIs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { logger } from '@microsoft/teams-js';
import React, { ReactElement } from 'react';

import { ApiWithoutInput } from './utils';
import { ModuleWrapper } from './utils/ModuleWrapper';

const TurnOnConsoleLog = (): React.ReactElement =>
ApiWithoutInput({
name: 'turnOnConsoleLog',
title: 'Turn On Console Log',
onClick: async () => {
logger.turnOnConsoleLog();
return 'true';
},
});

const TurnOffConsoleLog = (): React.ReactElement =>
ApiWithoutInput({
name: 'turnOffConsoleLog',
title: 'Turn Off Console Log',
onClick: async () => {
logger.turnOffConsoleLog();
return 'true';
},
});
const LoggerAPIs = (): ReactElement => (
<ModuleWrapper title="Logger">
<TurnOnConsoleLog />
<TurnOffConsoleLog />
</ModuleWrapper>
);

export default LoggerAPIs;
2 changes: 2 additions & 0 deletions apps/teams-test-app/src/pages/TestApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import GeoLocationAPIs from '../components/GeoLocationAPIs';
import HostEntityTabAPIs from '../components/HostEntityTabAPIs';
import Links from '../components/Links';
import LocationAPIs from '../components/LocationAPIs';
import LoggerAPIs from '../components/LoggerAPIs';
import LogAPIs from '../components/LogsAPIs';
import MailAPIs from '../components/MailAPIs';
import MarketplaceAPIs from '../components/MarketplaceAPIs';
Expand Down Expand Up @@ -124,6 +125,7 @@ export const TestApp: React.FC = () => {
<Links />
<LocationAPIs />
<LogAPIs />
<LoggerAPIs />
<MailAPIs />
<MarketplaceAPIs />
<MediaAPIs />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Added a capability for better logging discoverability",
"packageName": "@microsoft/teams-js",
"email": "[email protected]",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions packages/teams-js/src/internal/globalVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export class GlobalVars {
public static hostClientType: string | undefined = undefined;
public static clientSupportedSDKVersion: string;
public static printCapabilityEnabled = false;
public static turnOnConsoleLog = false;
}
55 changes: 54 additions & 1 deletion packages/teams-js/src/internal/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { debug as registerLogger, Debugger } from 'debug';

import { GlobalVars } from './globalVars';
import { UUID } from './uuidObject';

// Each teamsjs instance gets a unique identifier that will be prepended to every log statement
Expand All @@ -12,7 +13,59 @@ registerLogger.formatArgs = function (args) {
originalFormatArgsFunction.call(this, args);
};

const topLevelLogger = registerLogger('teamsJs');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const createDebuggerFunction = (namespace: string): Debugger => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool and it will probably need a good dose of explanatory comments

const internalDebugger: Debugger = registerLogger(namespace);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const func = function (formatter: any, ...args: any[]): void {
if (GlobalVars.turnOnConsoleLog && localStorage.getItem('debug') != 'teamsJs.*') {
args.unshift(`color: ${internalDebugger.color}`);
console.log(`${internalDebugger.namespace}: ${formatter}`, args);
}
internalDebugger(formatter, args);
} as Debugger;

Object.assign(func, {
get color() {
return internalDebugger.color;
},
set color(value: string) {
internalDebugger.color = value;
},
get diff() {
return internalDebugger.diff;
},
set diff(value: number) {
internalDebugger.diff = value;
},
get enabled(): boolean {
return internalDebugger.enabled;
},
set enabled(enabled: boolean) {
internalDebugger.enabled = enabled;
},
get namespace(): string {
return internalDebugger.namespace;
},
set namespace(namespace: string) {
internalDebugger.namespace = namespace;
},
extend(namespace: string, delimiter?: string): Debugger {
return createDebuggerFunction(internalDebugger.extend(namespace, delimiter).namespace);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
log(...args: any[]) {
internalDebugger.log(args);
},
});

return func;
};

const topLevelLogger: Debugger = createDebuggerFunction('teamsJs');

// const topLevelLogger = registerLogger('teamsJs');

/**
* @internal
Expand Down
1 change: 1 addition & 0 deletions packages/teams-js/src/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ export { settings } from './settings';
export { tasks } from './tasks';
export { liveShare, LiveShareHost } from './liveShareHost';
export { marketplace } from './marketplace';
export { logger } from './logger';
21 changes: 21 additions & 0 deletions packages/teams-js/src/public/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GlobalVars } from '../internal/globalVars';

export namespace logger {
/**
* todo: doc
* @returns bool
*/
export function turnOnConsoleLog(): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably no reason you need to return these booleans

GlobalVars.turnOnConsoleLog = true;
return GlobalVars.turnOnConsoleLog;
}

/**
* todo: doc
* @returns bool
*/
export function turnOffConsoleLog(): boolean {
GlobalVars.turnOnConsoleLog = false;
return GlobalVars.turnOnConsoleLog;
}
}
Loading